Rayrun
← Back to Discord Forum

Help with error "Unable to retrieve content because the page is navigating and changing the content"

Hi all, I am getting the following error "Unable to retrieve content because the page is navigating and changing the content" when doing the following:

  1. Executing JS (which causes a page navigation to occur)
  2. Trying to get the new page's source (using Page.ContentAsync()) <-- Error is happening here

I am using the .NET flavor of Playwright. I know it has to do with a 'waiting for navigation' thing, as the page isn't yet loaded (the browser is still 'spinning', but how would I handle this if I have a generic method called "GetPageSource()" that just returns back Page.ContentAsync() and doesn't know whether or not there is a navigation going on?

I know there is Page.WaitForURLAsync() but that is expecting the URL, which I wouldn't know.

Thanks in advance for any help.

This thread is trying to answer question "How to handle the error 'Unable to retrieve content because the page is navigating and changing the content' when trying to get the new page's source using Page.ContentAsync() in .NET Playwright?"

3 replies

would assert you will need to find something you can wait for...

what do you mean? can you provide a code example by any chance?

Hi, what if you don't make GetPageSource() responsible for the waiting, but instead make the function which executes the JavaScript responsible for the waiting? I suppose that function knows which JavaScript it's executing, and knows what it's doing (navigation), so that sounds to me to be the best place to also take care of ensuring that the navigation is ready before returning from that function.

What exactly to wait for depends on the app. Some kind of indication that the page is done rendering (which is harder nowadays since so much is happening asynchronously). But for example a login page may as a last step set focus to the username field. In such case you could wait for await page.locator('username:focus').waitFor()

Or you could wait for an API response which is always last: await page.waitForResponse(/last api response/)

But those are just examples.

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].