Rayrun
← Back to Discord Forum

Check which page has loaded

I'm using python. Clicking a button takes me to the desired page 90% of the time; however, 10% of the time I get an error page. The desired page contains a known locator. My goal is to understand which of the two pages has loaded. So my idea was to use page.get_by_role("button", name="click").click() on the button, then page.wait_for_load_state() or page.wait_for_load_state("domcontentloaded"), then check page.locator('known_locator').count() to see if it's 0 or 1. However, wait_for_load_state() doesn't seem to be working, so I'm forced to do page.wait_for_selector('known_locator', timeout=....) after the button has been clicked, but this forces a needless wait for the timeout when the error page is loaded, which takes time. Is there a way to make page.wait_for_load_state() work after a click?

This thread is trying to answer question "Is there a way to make `page.wait_for_load_state()` work after a click in Python?"

14 replies
laurent_autom
laurent_autom

One strategy is to use PlayWright's internal assertions. Furthermore, if you want your test to not fail because the wrong page loaded, why not use a try/catch on the assertion and customize the error message? https://playwright.dev/java/docs/test-assertions

but I guess even the assertion needs the page to load first?

another sample of what seems to be the same issue: page.get_by_role("button", name="clickme").click(); page.wait_for_load_state(); print(page.url) prints the URL of the page where the "clickme" button is, not the URL of the page where the button is supposed to take you.

laurent_autom
laurent_autom
@senwor: Yes, pw needs the next page to be loaded otherwise the locator cannot be activated and passed to assertions. I haven't played yet with not(); it could be interesting: https://playwright.dev/java/docs/api/class-locatorassertions#locator-assertions-not As your test is flaky, did you manage to understand its root cause? Maybe the devs could do something and fix the bug.

Yes, pw needs the next page to be loaded otherwise the locator cannot be activated and passed to assertions. I haven't played yet with not(); it could be interesting: https://playwright.dev/java/docs/api/class-locatorassertions#locator-assertions-not

As your test is flaky, did you manage to understand its root cause? Maybe the devs could do something and fix the bug.

No I didn't, that's why I'm asking here.

Do you not know what url your navigations result in? Is there a reason why page.wait_for_url("**/login") wouldn't work for your needs?

well, I can't disclose the actual details since it's a password-protected page inside an intranet, but see for example the bug above. The generated URL is virtually random and only known after the button has been clicked.

And a regex pattern wouldn't work?

in my real case, the generated url is quite different in structure when it works and when it fails, in one case it's something like https://blah.com/page2/foo and in the other case it's more like https://blah.com/errorpages/error106

I suppose I could somehow make a regex work, but I still don't understand why page.wait_for_load_state() which should actually do just that, ie wait, isn't working here

"If the state has been already reached while loading current document, the method resolves immediately."

well, obviously the state hasn't been reached, and if you run the code in the github bug you can see it for yourself

perhaps (well, most likely) it thinks it has reached the state, that's why it does nothing

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].