I'm using Playwright Java version 1.28.1 to test my company's website. I'm trying to navigate to the website using the following code:
page.waitForLoadState(LoadState.DOMCONTENTLOADED);
page.navigate("https://my-company-website.com/");
The problem is that the website also loads ads at the same time as the main app, and sometimes these ads take a long time to load. This prevents the DOMContentLoaded event from firing, and I get a TimeoutError:
com.microsoft.playwright.TimeoutError: Error { message='Timeout 30000ms exceeded.
=========================== logs ===========================
navigating to "https://my-company-website.com/", waiting until "load".
Is there a way to fix this error? Can I tell Playwright to not wait for the page to fully load? Or can I trigger the page load as soon as a specific HTML DOM element becomes visible?
Any help would be appreciated.
I've never used Playwright for Java, but in other language implementations, such as Playwright for JavaScript, usually you only navigate rather than waiting for a load state before a navigation. In JS, the following usually fails because of the extra wait:
// usually wrong
await page.waitForNavigation({waitUntil: "domcontentloaded"});
await page.goto("https://www.example.com", {waitUntil: "domcontentloaded"});
or
// usually wrong
await page.goto("https://www.example.com", {waitUntil: "domcontentloaded"});
await page.waitForNavigation({waitUntil: "domcontentloaded"});
Try making a single call to navigate
and specify the setWaitUntil
option to determine which load state the navigate
call should block until.
For example:
// removed waitForLoadState
page.navigate(
"https://example.com",
new Page.NavigateOptions()
.setWaitUntil(WaitUntilState.DOMCONTENTLOADED)
);
Ads shouldn't prevent the DOM loaded event from firing, but if they do, it'd be useful to see the actual site so the behavior can be understood and dealt with. A typical solution is to block requests that run the ad scripts. It's generally a good idea to block any unneeded resources for performance reasons anyway.