【问题标题】:How to wait for sometime once the page is loaded using selenium webdriver?使用 selenium webdriver 加载页面后如何等待某个时间?
【发布时间】:2016-11-11 06:40:03
【问题描述】:

我需要等待一段时间,因为页面需要时间来加载。我需要隐含地等待。如何使用 selenium webdriver java 来完成?

【问题讨论】:

标签: java selenium-webdriver automation wait


【解决方案1】:

请试试这个。

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.google.com");

【讨论】:

    【解决方案2】:

    配置驱动程序以隐式等待页面加载。

    隐式等待是告诉 WebDriver 在尝试查找一个或多个元素(如果它们不是立即可用的)时轮询 DOM 一段时间。默认设置为 0。一旦设置,就会为 WebDriver 对象实例的生命周期设置隐式等待。

    WebDriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //makes driver object to wait for 10 seconds to wait implicitly
    driver.get("http://somedomain/url_that_delays_loading");
    

    或者您可以定义 ExplicitCondition 以等待确认页面加载的特定事件发生。

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("someid"))); // give an element locator, such a way that you can confirm that visibility of that elements represents the complete loading of the page.
    

    这会在抛出 TimeoutException 之前等待最多 10 秒,或者如果它发现元素将在 0 - 10 秒内返回它。默认情况下,WebDriverWait 每 500 毫秒调用一次 ExpectedCondition,直到它成功返回。 ExpectedCondition 函数类型的成功返回值是布尔值 true 或非空对象。

    注意:根据您的要求配置超时(在示例中为 10 秒)。

    参考:

    1. http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp

    【讨论】:

      猜你喜欢
      • 2014-09-26
      • 2012-07-07
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      相关资源
      最近更新 更多