How to wait for page to load in Playwright?

You can wait for the page to load in Playwright by making use of the wait_for_selector method of the Page object. By default, Playwright will pause before the page has fully loaded but this does not take into account any XHR or AJAX requests triggered after the page load. You can account for those by using the wait_for_selector method and waiting for an element that confirms the page has fully loaded.

Here is some sample code that searches for the hottest sneakers on Pinterest and then waits for the pins to show up before saving a screenshot of the page:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False)
    page = browser.new_page()

    # Go to Pinterest
    page.goto("https://www.pinterest.com/search/pins/?rs=ac&len=2&q=hottest%20sneakers")

    # Wait for the pins to show up
    page.wait_for_selector("div[data-grid-item=true]")
    
    # Save the screenshot
    page.screenshot(path="pinterest.png")

Related Playwright web scraping questions: