1

I want selenium to force the browser to reload the page which it is loading if the loading process takes too long.

From StackOverflow I have that this code

new WebDriverWait(driver, 30).until((ExpectedCondition<Boolean>) wd -> ((JavascriptExecutor) wd)
                .executeScript("return document.readyState").equals("complete"));

will wait until the page is fully loaded, but I want it to be reloaded if it takes more than 30 seconds.

How can I achieve that?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
assembler
  • 3,098
  • 12
  • 43
  • 84
  • there is a PageLoadTimeout: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html – pcalkins Aug 12 '19 at 21:16
  • 1
    Just to clarify, this WebDriverWait method is for client-side page updates. For full page loads from the server, PageLoadTimeout comes into play. – pcalkins Aug 12 '19 at 22:19

3 Answers3

1

WebDriverWait with through timeout exception. Put your code inside try/catch and reload the page on timeout exception:

try {
    new WebDriverWait(driver, 30).until((ExpectedCondition<Boolean>) wd -> ((JavascriptExecutor) wd)
            .executeScript("return document.readyState").equals("complete"));
} catch (TimeoutException e) {
    // log a timeout
    // System.out.println("Page load timeout, refresh.");
    driver.navigate().refresh();
}
Sers
  • 12,047
  • 2
  • 12
  • 31
1

To reload the webpage incase the loading process is taking too long you can configure pageLoadTimeout. pageLoadTimeout sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.

An example (using Selenium v3.141.59 and GeckoDriver v0.24.0):

  • Code Block:

    public class 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);
            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");
              // do your other work here
            }catch(WebDriverException e){
              System.out.println("WebDriverException occured");
              }
            driver.quit();
        }
    }
    
  • Console Output:

    1565680787633   mozrunner::runner   INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-foreground" "-no-remote" "-profile" "C:\\Users\\Debanjan.B\\AppData\\Local\\Temp\\rust_mozprofile.3jw3aiyfNAiQ"
    1565680826515   Marionette  INFO    Listening on port 56499
    1565680827329   Marionette  WARN    TLS certificate errors will be ignored for this session
    Aug 13, 2019 12:50:28 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C
    Aug 13, 2019 12:50:31 PM org.openqa.selenium.remote.ErrorCodes toStatus
    WebDriverException occured
    
  • You can find a relevant discussion in pageLoadTimeout in Selenium not working

You can find a detailed discussion in Do we have any generic function to check if page has completely loaded in Selenium

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Try _driver.Navigate().Refresh();

Qingshan
  • 388
  • 3
  • 5