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

[Bug]: Playwright hangs on lazy-(not)loaded iframe

Open conte91 opened this issue 1 month ago • 2 comments

Version

1.55.0

Steps to reproduce

  1. Run this test script https://gist.github.com/conte91/2ce2d9d4ef4ff75b378e2e07df4ec68a
  2. The script spins a HTTP server that serves a page (index.html) containing an iframe. The iframe is set to lazy loading, and won't be fetched until it's displayed by clicking the html button.
  3. playwright shows an empty URL on the not-yet-loaded iframe
  4. running frame.evaluate hangs the execution
  5. Clicking on the button while the script is running (so the iframe is loaded) unblocks the execution, JS runs, script completes successfully.

Expected behavior

One of the following:

  • There's a well-defined/documented way to know that the iframe doesn't have an execution context yet and it will hang the script
  • The frame.evaluate call returns an error immediately saying the iframe doesn't "exist" yet
  • Calling frame.evaluate forces the frame to load, then executes the code

Actual behavior

Script hangs, there seems to be no way to know in advance that the iframe wasn't loaded (except for checking if the URL is empty, but that seems to be an unreliable/brittle side effect, I'm not sure is the intended way to perform this check)

Additional context

This seems to be very much related to https://github.com/microsoft/playwright/issues/9675, https://github.com/microsoft/playwright/issues/8943, which we have encountered in the past.

From whatwg's specs (this section) it seems to me that an iframe should never report an empty URL (a frame which hasn't loaded should maybe return about:blank?). If this behaviour is expected, then I wonder if there's a programmatic way to know in advance if the frame is loaded or not, or if a frame having an empty URL is indeed the "correct" way to know that the iframe isn't loaded (and an iframe with an empty URL can only be encountered in this scenario).

We discovered this since we have a test that runs some parsing code on google's home page. today's page shows the halloween doodle, and the test was hanging -> today's halloween doodle successfully spooked us :)

Environment

- Operating System: Ubuntu 22.04
- CPU: amd64
- Browser: Chromium, maybe others? (only tested on chromium)
- Python Version: [3.12.9]
- Other info: Also tried on other OS/stacks (e.g. arch, python 3.11), seems to make no difference

conte91 avatar Oct 31 '25 18:10 conte91

hi,I've encountered this problem too. Are there any solutions?

selfshore avatar Nov 11 '25 12:11 selfshore

@selfshore our current workaround is just to avoid evaluating any JS when frame.url == ''. We use this as a proxy signal for "frame hasn't loaded yet", but (see context above) it seems brittle. "works alright" for our use case :)

conte91 avatar Nov 13 '25 02:11 conte91