如果加载时间过长,如何让 selenium 重新加载所需的 url [英] How to make selenium to reload the desired url if it takes too long loading

查看:65
本文介绍了如果加载时间过长,如何让 selenium 重新加载所需的 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果加载过程太长,我希望 selenium 强制浏览器重新加载正在加载的页面.

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

从 StackOverflow 我有这个代码

From StackOverflow I have that this code

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

将等到页面完全加载,但如果超过 30 秒,我希望它重新加载.

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?

推荐答案

要重新加载网页以防加载过程花费太长时间,您可以配置 pageLoadTimeout.pageLoadTimeout 设置在抛出错误之前等待页面加载完成的时间.如果超时为负,则页面加载可能是不确定的.

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.

示例(使用 Selenium v​​3.141.59 和 GeckoDriver v0.24.0):

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
    

  • 您可以在 pageLoadTimeout in Selenium not working 中找到相关讨论

    您可以在 我们是否有任何通用函数来检查页面是否已在 Selenium 中完全加载

    这篇关于如果加载时间过长,如何让 selenium 重新加载所需的 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆