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

允我心安 提交于 2019-12-24 08:07:14

问题


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?


回答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




回答2:


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();
}



回答3:


Try _driver.Navigate().Refresh();



来源:https://stackoverflow.com/questions/57468213/how-to-make-selenium-to-reload-the-desired-url-if-it-takes-too-long-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!