Page Loading Strategy in the Selenium Webdriver

Reading Time: 2 minutes

Hello everyone, in this blog we are going to see what is page loading strategy in selenium web driver.

What is Page Loading Strategy in Selenium?

  • This page loading strategy will come into the picture whenever you are launching your browser using the get() method or navigate.to() method.
  • By default, Selenium WebDriver follows the standard page load strategy when it loads any page which means it waits until the entire page is loaded.
  • Page Loading Strategy can be helpful in speeding up the process of web automation.
driver.get(https://www.google.com);

Whenever you launch your URL, so what will happen? This method will get hold off with your session. It will not give control to your driver until the complete page loads.

  • Every browser supports the Ready State method, so selenium uses this ready state method.
  • By default Selenium will wait until the Document’s Ready State is “complete.”
  • The document.readyState property of a document describes the loading state of the current document.
  • By default, WebDriver will hold off on completing a navigation method (e.g., driver.navigate().get()) until the document ready state is complete.
  • This does not necessarily mean that the page has finished loading, especially for sites like Single Page Applications that use a lot of JavaScript to dynamically load content after the Ready State returns complete.
  • Note also that this behavior does not apply to navigation that is a result of clicking an element or submitting a form.

WebDriver page load strategy supports the following values:

  1. Normal
  2. Eager
  3. None

Normal

  • This will make Selenium WebDriver wait for the entire page to be loaded.
  • When set to normal, Selenium WebDriver waits until the load event fire is returned.
  • By default normal is set to the 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://www.google.com");
    }

}

Eager

  • This will make Selenium WebDriver wait until the initial HTML document has been completely loaded and parsed, and discards the loading of stylesheets, images, and subframes.
  • When set to eager, Selenium WebDriver waits until DOMContentLoaded event fire is returned.
  • Finds the elements even before 100% page has been rendered.
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://www.google.com");

    }
}

None

  • When set to none Selenium WebDriver only waits until the initial page is downloaded.
  • Finds element after 100% page has loaded.
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://www.google.com")

    }
}
StrategyReady StatesNotes
NormalcompleteUsed by default, waits for all resources to download
EagerinteractiveDOM access is ready, but other resources like images may still be loading
NoneanyDoes not block WebDriver at all
Page Load Strategy
In addition, ChromeDriver 77.0 (which supports Chrome version 77) now supports eager as pageLoadStrategy.

Conclusion

Conclusively, when Page Loading takes too much time and you need to stop downloading additional sub-resources (images, CSS, js, etc) you can change the page load strategy through the web driver. By default, when Selenium loads a page, it follows the normal page load strategy.

Reference

https://www.selenium.dev/documentation/webdriver/capabilities/shared/

Written by 

I am Software Consultant - QA at Knoldus Inc. I have completed my B.Tech from Jodhpur Institute of Engineering and Technology. I am familiar with the core concepts of Manual and Automation Testing and tools such as Postman, Selenium, and Gatling. I am looking forward to roles that will help me realize my potential by exploring the various aspects of this field.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading