Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page load strategy for Chrome driver (Updated till Selenium v3.12.0)

I'm using Chrome browser for testing WebApp.

Sometimes pages loaded after very long time. I needed to stop downloading or limit their download time.

In FireFox I know about PAGE_LOAD_STRATEGY = "eager".

Is there something similar for chrome?

P.S.: driver.manage().timeouts().pageLoadTimeout() works, but after that any treatment to Webdriver throws TimeOutException. I need to get the current url of the page after stopping its boot.

like image 638
PanamaBoy Avatar asked May 02 '17 09:05

PanamaBoy


People also ask

What is the default page load strategy?

NORMAL. Indicates WebDriver should wait for the document readiness state to be "complete" after navigation. This is the default page loading strategy.

Which version of Selenium is compatible with Chrome?

ChromeDriver is only compatible with Chrome version 12.0. 712.0 or newer. If you need to test an older version of Chrome, use Selenium RC and a Selenium-backed WebDriver instance.

What is pageLoadStrategy Selenium?

As of this writing, pageLoadStrategy supports the following values : normal. This stategy causes Selenium to wait for the full page loading (html content and subresources downloaded and parsed). eager. This stategy causes Selenium to wait for the DOMContentLoaded event (html content downloaded and parsed only).


Video Answer


1 Answers

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

Resolved issue 1902: Support eager page load strategy [Pri-2]


From the Webdriver specs:

For commands that cause a new document to load, the point at which the command returns is determined by the session’s page loading strategy.

When Page Loading takes too much time and you need to stop downloading additional subresources (images, css, js etc) you can change the pageLoadStrategy through the webdriver.

As of this writing, pageLoadStrategy supports the following values :

  1. normal

    This stategy causes Selenium to wait for the full page loading (html content and subresources downloaded and parsed).

  2. eager

    This stategy causes Selenium to wait for the DOMContentLoaded event (html content downloaded and parsed only).

  3. none

    This strategy causes Selenium to return immediately after the initial page content is fully received (html content downloaded).

By default, when Selenium loads a page, it follows the normal pageLoadStrategy.


Here is the code block to configure pageLoadStrategy() through both an instance of DesiredCapabilities Class and ChromeOptions Class as follows : :

  • Using DesiredCapabilities Class :

    package demo; //replace by your own package name
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.remote.DesiredCapabilities;
    
    public class A_Chrome_DCap_Options {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            DesiredCapabilities dcap = new DesiredCapabilities();
            dcap.setCapability("pageLoadStrategy", "normal");
            ChromeOptions opt = new ChromeOptions();
            opt.merge(dcap);
            WebDriver driver = new ChromeDriver(opt);
            driver.get("https://www.google.com/");
            System.out.println(driver.getTitle());
            driver.quit();
        }
    }
    
  • Using ChromeOptions Class :

    package demo; //replace by your own package name
    
    import org.openqa.selenium.PageLoadStrategy;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    
    
    public class A_Chrome_Options_test {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions opt = new ChromeOptions();
            opt.setPageLoadStrategy(PageLoadStrategy.NORMAL);
            WebDriver driver = new ChromeDriver(opt);
            driver.get("https://www.google.com/");
            System.out.println(driver.getTitle());
            driver.quit();
        }
    }
    

Note : pageLoadStrategy values normal, eager and none is a requirement as per WebDriver W3C Editor's Draft but pageLoadStrategy value as eager is still a WIP (Work In Progress) within ChromeDriver implementation. You can find a detailed discussion in “Eager” Page Load Strategy workaround for Chromedriver Selenium in Python


References:

  • WebDriver navigation
  • WebDriver page load strategies
  • WhatWG Document readyStateChange / readiness
like image 194
undetected Selenium Avatar answered Oct 01 '22 15:10

undetected Selenium