Page Load Strategy in Selenium WebDriver

By default, Selenium WebDriver follows the normal pageLoadStrategy when it loads any page which means it waits until the entire page is loaded. In scenarios where it takes a lot of time to load the page, it is always recommended to stop downloading additional resources like images, css, js, etc… by using the eager pageLoadStrategy.

In single page applications like Angular and React, once the dynamic content is already loaded then clicking on  a link or performing some action within the page will not make a new request to the server as the content is dynamically loaded at the client side without a full page refresh. These applications  can load many views dynamically without any server requests, so pageLoadStrategy will always show Complete status which is not always true and it leads to failure in Selenium scripts.

WebDriver pageLoadStrategy supports the following values:

1. NORMAL

This will make Selenium WebDriver to wait for the entire page is loaded. When set to normal, Selenium WebDriver waits until the load event fire is returned. By default normal is set to browser if none is provided.

public class pageLoadStrategy {
    public static void main(String[] args) {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.get("https://qascript.com");
    }
}

2. EAGER

This will make Selenium WebDriver to wait until the initial HTML document has been completely loaded and parsed, and discards loading of stylesheets, images and subframes. When set to eager, Selenium WebDriver waits until DOMContentLoaded event fire is returned.

public class pageLoadStrategy {
    public static void main(String[] args) {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.get("https://qascript.com");
    }
}

3. NONE

When set to none Selenium WebDriver only waits until the initial page is downloaded.

public class pageLoadStrategy {
    public static void main(String[] args) {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.get("https://qascript.com")
    }
}

Note:

ChromeDriver 77.0 (which supports Chrome version 77) now supports eager as pageLoadStrategy.

Share

Bijan Patel

Founder & Creator of QAScript | 12+ years of IT Experience | Full Stack Automation Engineer | Blogger | Trainer

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!