javaseleniumselenium-webdriverwebdriverpageloadtimeout

How to make selenium to reload the desired url if it takes too long loading


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?


Solution

  • 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):

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