pageLoadTimeout is not working in Selenium - Java

戏子无情 提交于 2019-11-29 15:48:06
Kireeti Annamaraj

You can set the pageload strategy for browser which will then make the page not wait for the full page load for your other Selenium commands to be executed. Below is the sample code snippet in Java. There are three supported 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).

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.

DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("pageLoadStrategy", "eager");
FirefoxOptions opt = new FirefoxOptions();
opt.merge(caps);
WebDriver driver = new FirefoxDriver(opt);
driver.get("https://www.google.com/");

If you are interested only in the HTML of the page, better use the "eager" strategy.

You haven't mentioned the url you are trying to access but pageLoadTimeout for Selenium works as expected with With Selenium v3.14.0, GeckoDriver v0.23.0 and Firefox Quantum v62.0.3 combination. I am able to see the expected output on the console with the following example which prints TimeoutException occurred. Quiting the program whenever the pageLoadTimeout is triggered:

  • Code Block:

    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.TimeoutException;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class A_Firefox_Test 
    {
        public static void main(String[] args) 
        {
            System.setProperty("god.bless.us", "C:/Utility/BrowserDrivers/geckodriver.exe");
            WebDriver driver = new FirefoxDriver();
            driver.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS);
            try {
                driver.get("https://www.booking.com/hotel/in/the-taj-mahal-palace-tower.html?label=gen173nr-1FCAEoggJCAlhYSDNiBW5vcmVmaGyIAQGYATG4AQbIAQzYAQHoAQH4AQKSAgF5qAID;sid=338ad58d8e83c71e6aa78c67a2996616;dest_id=-2092174;dest_type=city;dist=0;group_adults=2;hip_dst=1;hpos=1;room1=A%2CA;sb_price_type=total;srfid=ccd41231d2f37b82d695970f081412152a59586aX1;srpvid=c71751e539ea01ce;type=total;ucfs=1&#hotelTmpl");
            } catch (TimeoutException e) {
                System.out.println("TimeoutException occurred. Quiting the program.");
            }
            driver.quit();
        }
    }
    
  • Console Output:

    1539157195615   Marionette  INFO    Listening on port 1920
    Oct 10, 2018 1:09:56 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C
    Oct 10, 2018 1:10:00 PM org.openqa.selenium.remote.ErrorCodes toStatus
    INFO: HTTP Status: '500' -> incorrect JSON status mapping for 'timeout' (408 expected)
    TimeoutException occurred. Quiting the program.
    
  • You can find the detailed stack trace in pageLoadTimeout in Selenium not working

  • You can find the Pythonic approach to pageLoadTimeout in How to set the timeout of 'driver.get' for python selenium 3.8.0?
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!