Search code examples
pythonplaywright

Playwright won't navigate to URL (Python)


The code below results in a timeout error on line 22. The page it gets hung up on loads fine. I don't think the "click" is working for whatever reason. I checked out this question and it didn't help: Navigating to "url", waiting until "load" - Python Playwright Issue

My goal is to download the csv file. I can't directly link to it since it's dependent on information entered during a user's session.

from playwright.sync_api import Playwright, sync_playwright, expect


def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()

    # Open new page
    page = context.new_page()

    # Go to https://www.elections.il.gov/CampaignDisclosure/ContributionSearchByCommittees.aspx?T=637994490317517425
    page.goto("https://www.elections.il.gov/CampaignDisclosure/ContributionSearchByCommittees.aspx?T=637994490317517425")

    # Click input[name="ctl00\$ContentPlaceHolder1\$txtCmteID"]
    page.locator("input[name=\"ctl00\\$ContentPlaceHolder1\\$txtCmteID\"]").click()

    # Fill input[name="ctl00\$ContentPlaceHolder1\$txtCmteID"]
    page.locator("input[name=\"ctl00\\$ContentPlaceHolder1\\$txtCmteID\"]").fill("34589")
    
    # Click input:has-text("Search")
    page.locator("input:has-text(\"Search\")").click()
    page.wait_for_url("https://www.elections.il.gov/CampaignDisclosure/ContributionSearchByCommittees.aspx?txtCmteID=%2fVrwBYk8TlTSLyRmy7TWmQ%3d%3d&ddlVendorState=Ry707kcsXsM%3d&ddlContributionType=wOGh3QTPfKqV2YWjeRmjTeStk426RfVK&ddlState=Ry707kcsXsM%3d&ddlFiledDateTime=Ry707kcsXsM%3d&ddlFiledDateTimeThru=Ry707kcsXsM%3d&T=637999702238350506")

    # Click text=Download This List
    page.locator("text=Download This List").click()
    page.wait_for_url("https://www.elections.il.gov/CampaignDisclosure/DownloadList.aspx?T=637999702324593366")

    # Click text=CSV File
    with page.expect_download() as download_info:
        page.locator("text=CSV File").click()
    download = download_info.value

    # ---------------------
    context.close()
    browser.close()


with sync_playwright() as playwright:
    run(playwright)

Copy of error message:

Exception has occurred: TimeoutError
Timeout 30000.0ms exceeded.
=========================== logs ===========================
waiting for navigation to "https://www.elections.il.gov/CampaignDisclosure/ContributionSearchByCommittees.aspx?txtCmteID=%2fVrwBYk8TlTSLyRmy7TWmQ%3d%3d&ddlVendorState=Ry707kcsXsM%3d&ddlContributionType=wOGh3QTPfKqV2YWjeRmjTeStk426RfVK&ddlState=Ry707kcsXsM%3d&ddlFiledDateTime=Ry707kcsXsM%3d&ddlFiledDateTimeThru=Ry707kcsXsM%3d&T=637999702238350506" until 'load'

Solution

  • I guess you are using codegen probably.

    That page is a little bit strange for urls as I can see.

    Anyways, I fixed your code:

    from playwright.sync_api import Playwright, sync_playwright, expect
    
    
    def run(playwright: Playwright) -> None:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context()
    
        # Open new page
        page = context.new_page()
    
        # Go to https://www.elections.il.gov/CampaignDisclosure/ContributionSearchByCommittees.aspx?T=637994490317517425
        page.goto(
            "https://www.elections.il.gov/CampaignDisclosure/ContributionSearchByCommittees.aspx")
    
        # Fill input[name="ctl00\$ContentPlaceHolder1\$txtCmteID"]
        page.locator("input[name=\"ctl00\\$ContentPlaceHolder1\\$txtCmteID\"]").fill("34589")
    
        page.locator("input:has-text(\"Search\")").click()
    
        page.locator("text=Download This List").click()
    
        # Click text=CSV File
        with page.expect_download() as download_info:
            page.locator("text=CSV File").click()
        download = download_info.value
        download.save_as(download.suggested_filename)
    
    
        # ---------------------
        context.close()
        browser.close()
    
    
    with sync_playwright() as playwright:
        run(playwright)