5

I am using below code using selenium in Java. I am adding pageLoadTimeout of 4 seconds, however, the driver continues to wait till the complete page is loaded. Any help?

System.setProperty("webdriver.gecko.driver", System.getProperty("user.home") + "\\Desktop\\geckodriver.exe");

        FirefoxBinary b = new FirefoxBinary(new File(System.getProperty("user.home") + "\\desktop\\Mozilla Firefox\\firefox.exe"));
        FirefoxOptions options = new FirefoxOptions().setBinary(b);
        driver = new FirefoxDriver(options);
        //driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(4, TimeUnit.SECONDS);


        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");
List<WebElement> facilitySectionList = driver.findElements(By.className("facilitiesChecklistSection"));
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Bharat Nanwani
  • 653
  • 4
  • 11
  • 27
  • It took around 4-5 sec in my side to load full page. is your internet connectivity is fine? – Shubham Jain Aug 09 '17 at 13:00
  • maybe the office network is causing some problem in loading the page. Nevertheless, any ideas on the timeout thing? – Bharat Nanwani Aug 09 '17 at 13:15
  • Wait for [**`Support a timeout argument on page load operations`**](https://github.com/SeleniumHQ/selenium-google-code-issue-archive/issues/687) to get resolved for `pageLoadTimeout`. – undetected Selenium Aug 09 '17 at 15:53

2 Answers2

4

The solution to your pageLoadTimeout issue would be to bump up your Selenium version to v3.5.0. Here is effective code block and the resulted org.openqa.selenium.TimeoutException: Timeout loading page after 2000ms of your own code in minimal lines:

  • Code block:

    public class Q45591282_pageloadtimeout 
    {
        public static void main(String[] args) 
        {
              System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
              WebDriver driver=new FirefoxDriver();
              driver.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS);
              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");
        }
    }
    
  • Console Output:

    1502530864350   geckodriver INFO    geckodriver 0.18.0
    1502530864365   geckodriver INFO    Listening on 127.0.0.1:29688
    1502530865042   geckodriver::marionette INFO    Starting browser C:\Program Files\Mozilla Firefox\firefox.exe with args ["-marionette"]
    1502530903170   Marionette  INFO    Listening on port 1900
    Aug 12, 2017 3:11:44 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C
    Exception in thread "main" org.openqa.selenium.TimeoutException: Timeout loading page after 2000ms
    Build info: version: '3.5.0', revision: '8def36e068', time: '2017-08-10T23:00:22.093Z'
    System info: host: 'ATECHM-03', ip: '192.168.1.48', os.name: 'Windows 8', os.arch: 'amd64', os.version: '6.2', java.version: '1.8.0_77'
    Driver info: org.openqa.selenium.firefox.FirefoxDriver
    Capabilities [{moz:profile=C:\Users\ATECHM~1\AppData\Local\Temp\rust_mozprofile.LSsvaNqlDbxE, rotatable=false, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0.0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=53.0, platformVersion=6.2, moz:processID=3652.0, browserName=firefox, javascriptEnabled=true, platformName=windows_nt}]
    Session ID: 8b841376-00fd-4359-8cae-a68912b23706
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:185)
        at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:120)
        at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
        at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:641)
        at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:368)
        at demo.Q45591282_pageloadtimeout.main(Q45591282_pageloadtimeout.java:20)
    

Catching the WebDriverException

  • Code Block:

    public class pageLoadTimeout 
    {
        public static void main(String[] args) 
        {
              System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
              WebDriver driver = new ChromeDriver();
              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(WebDriverException e){
                  System.out.println("WebDriverException occured");
              }
              driver.quit();
        }
    }
    
  • Console Output:

    Only local connections are allowed.
    Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
    Jul 17, 2019 8:53:26 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C
    [1563377008.449][SEVERE]: Timed out receiving message from renderer: 1.999
    [1563377008.450][SEVERE]: Timed out receiving message from renderer: -0.001
    [1563377008.461][SEVERE]: Timed out receiving message from renderer: -0.012
    [1563377010.466][SEVERE]: Timed out receiving message from renderer: 1.998
    [1563377010.467][SEVERE]: Timed out receiving message from renderer: -0.001
    [1563377010.476][SEVERE]: Timed out receiving message from renderer: -0.010
    WebDriverException occured
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • This is works for me! So I guess, Selenium version 3.4.0 has some bug related to pageTimeOut method. and version 3.5.0 is currently not available in Maven. I manually added the jars and works perfectly! Thanks again. :) – Bharat Nanwani Aug 14 '17 at 07:51
  • @BharatNanwani Yep :) `pageLoadTimeout()` got fixed in v3.5.0 but in v3.4.0 we did had some work around to implement `pageLoadTimeout()` feature in some other way. Glad to be able to help you. – undetected Selenium Aug 14 '17 at 08:10
  • @DebanjanB i am using the latest selenium-java v3.141.59 and above solution didn't help me. I think [This question](https://stackoverflow.com/questions/60390926/pageloadtimeout-not-working-for-firefox-browser-in-selenium) that you have closed is not similar to above question. – Suban Dhyako Feb 26 '20 at 09:02
  • Above question is related to `the driver continues to wait till the complete page is loaded` but mine is just opposite that is `the driver doesn't wait till the complete page is loaded`. Mine issue only occured in firefox browser. – Suban Dhyako Feb 26 '20 at 09:10
0

if you specify the time, and selenium is not able to load the site is specified time, it will throw TimedoutException. You should handle that in your code.

    driver.manage().timeouts().pageLoadTimeout(4, TimeUnit.SECONDS);
    long startTime = System.currentTimeMillis();
    try{
        driver.get("https://yahoo.com");
    }catch (TimeoutException te){
        long estimatedTime = System.currentTimeMillis() - startTime;
        System.out.println("it took "+estimatedTime+" Time");
    }

if it's not throwing TimeoutException, either website has done loading within specified time or something is wrong with Selenium API.

Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137