Search code examples
pythonweb-scrapingplaywrightplaywright-python

Get Playwright to Wait Until Title Contains Specific Text


I'm new at playwright and I'm a little stuck. I'm scraping a site that has a lot of back and forth Javascript. I want my parsing logic to wait until the the title of the page is populated with a particular value, which would indicate that the page has loaded enough to parse.

Here are the relevant parts of my code.

        with sync_playwright() as pw_firefox:
            browser = pw_firefox.firefox.launch(headless=True, timeout=self.timeout)
            context = browser.new_context(viewport={"width": 1920, "height": 1080},
                                          extra_http_headers=HEADERS,
                                          strict_selectors=False)
            page = context.new_page()

            # Go to url and wait for the page to load
            page.goto(final_url)
            html = page.content()

Is there a way to use the wait_for_selector() or wait_for_function() methods to get playwright to wait until the <title> is populated with a specific value? Thanks!


Solution

  • "To wait until the title of the page is populated "

    To address above problem statement,you may simply use page assertion to_have_title:

    expect(page).to_have_title("new title",timeout=5000)