πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
Back to All Scenarios
Scenario 7 of 100
Synchronization
Intermediate

Handling Extremely Slow Page Loads

⏳Scenario Overview

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.

SeleniumAutomation.java
Selenium 4 + Java
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")
    );
}