playwright-python icon indicating copy to clipboard operation
playwright-python copied to clipboard

[Bug]: When the page.route() function is set, it fails to intercept requests, causing them to stall when the document of the page is new.

Open pengdaCN opened this issue 1 year ago • 1 comments

Version

1.41.2

Steps to reproduce

browser = pw.chromium.launch( devtools=True, headless=False, )

context = browser.new_context()

page = context.new_page() page.route("**/*", lambda route: route.continue_()) page.goto('https://www.baidu.com') time.sleep(5) page.goto('https://www.jd.com')

time.sleep(1000)

Expected behavior

after navigating to https://www.jd.com/, the previous images on the page, as well as other resources, load correctly, and it is possible to click on the page to open new pages normally.

Actual behavior

after navigating to https://www.jd.com/, only some resources load, while the loading of additional resources feels completely stuck. Moreover, when clicking on the page to open new pages, the resource loading for these new pages also gets stuck.

Additional context

No response

Environment

- Operating System: windows 10
- CPU: amd64
- Browser: Chromium
- Python Version: 3.11
- Other info:

pengdaCN avatar Feb 21 '24 10:02 pengdaCN

Looks like this is about: https://playwright.dev/python/docs/library#timesleep-leads-to-outdated-state

Could you try using page.wait_for_timeout(1000) instead?

mxschmitt avatar Feb 21 '24 15:02 mxschmitt

I think The issue is with time.sleep() You can Use This Code If It Helps

browser = pw.chromium.launch(
    devtools=True,  # Consider removing devtools=True for production
    headless=False,
)

context = browser.new_context()

page = context.new_page()
page.route("**/*", lambda route: route.continue_())
page.goto('https://www.baidu.com')

# Instead of time.sleep(), you can use page.wait_for_timeout() like this:
page.wait_for_timeout(5000)  # Wait for 5 seconds (5000 milliseconds)

page.goto('https://www.jd.com')
# You can wait for specific elements to appear using wait_for_selector() method
page.wait_for_selector('YOUR_SELECTOR_HERE')

# No need for time.sleep() here, as we've used asynchronous waiting mechanisms

zafranhaider avatar Mar 03 '24 22:03 zafranhaider

Closing as per above.

mxschmitt avatar Mar 04 '24 06:03 mxschmitt