Handling Extremely Slow Page Loads
Handling Extremely Slow Page Loads
Key Takeaways & Cheat Sheet
- βConfigure custom pageLoadTimeout values on the driver object
- βUtilize EAGER page load strategy to ignore slow trackers and styling
- βImplement programmatical Javascript waits for document.readyState
- βTrack load times and capture network HAR logs for bottleneck analysis
Short Direct Answer
Selenium's default page load strategy blocks the test thread until the browser completely fetches and parses all subresources (images, third-party trackers, scripts). If a slow tracker hangs, the entire test blocks and fails with a page load timeout. You can optimize this by changing the Page Load Strategy to "eager" (which proceeds once the HTML DOM is interactive) or waiting for document.readyState via Javascript.
β οΈ Senior Warning (Red Flag)
Avoid inflating your pageLoadTimeout limit (e.g. to 120 seconds) to bypass slow loading. This masks real performance issues in your application. Instead, switch to eager loading or consult the development team.
π‘ STAR Deep Dive Explanation & Pro Tip
PageLoadStrategy.EAGER is a game changer for sites with slow ads or analytics trackers. It allows your tests to run the moment the functional HTML layout is ready.
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.chrome.ChromeOptions;
// β
Set Page Load Strategy to EAGER (Stops waiting for images/trackers)
ChromeOptions options = new ChromeOptions();
options.setPageLoadStrategy(PageLoadStrategy.EAGER);
WebDriver driver = new ChromeDriver(options);
// β
Configure explicit page load timeout limit (e.g., 20 seconds)
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(20));
// β
Wait programmatically for Javascript document load completion
public void waitForPageToLoad() {
new WebDriverWait(driver, Duration.ofSeconds(15)).until(
d -> ((JavascriptExecutor) d)
.executeScript("return document.readyState").equals("complete")
);
}