【问题标题】:Implicitwait not waiting for the specified duration in the code mentioned below隐式等待不等待下面提到的代码中的指定持续时间
【发布时间】:2016-08-04 08:37:16
【问题描述】:

Selenium webdriver 必须等待代码中提到的 30 秒和 5 秒。但注意到 webdriver 正在跳过它。是什么原因以及如何让 webdriver 等待。

System.out.println("Before 5"+date.toString());

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

System.out.println("After 5"+date.toString());

完整代码的链接可以在这里找到 https://drive.google.com/file/d/0B4SgyzyvwKhiUk9KVldTa2ZGUkE/view?usp=sharing

【问题讨论】:

  • 您能告诉我您说它不等待的依据是什么吗?是否在不等待 5 秒的情况下抛出 'NoSuchElememt' 或 element not found execption?
  • 没有错误。通过打印等待前和等待后的时间来计算。
  • 请查看发布的答案。

标签: selenium selenium-webdriver


【解决方案1】:

隐式等待不像正常的 Thread.sleep() 那样工作,您在其中设置了 5s 时间,您的主线程被暂停并等待 5 秒。

它将与WebDriver Instance 一起使用,它将等待网页上的特定元素出现在等待中提到的时间。如果没有要识别的元素,即元素已加载到 dom 中,则您的隐式等待不会等待该时间段。

所以当你说:-

System.out.println("Before 5"+date.toString());
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
System.out.println("After 5"+date.toString());

它不会显示 5 秒的时差。

如果有任何元素没有加载到 dom 中,它会等待 5 秒。

System.out.println("Before 5"+date.toString());
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.id("some id")).sendKeys("Some Text")
System.out.println("After 5"+date.toString());  |
                                                |------- Here if the element isn't loaded in dom, then webdriver would wait for 5 seconds.

更多关于Waits的信息

【讨论】:

  • 嗨@pArAs,感谢您的回答。有一个建议。最近我收到了来自 SO 的评论,要求不要参考外部网站来回答。如果将来外部页面被删除,用户在引用线程时可能很难得到正确的答案。所以建议把有用的内容放在答案本身。
  • 这就是为什么我修改了我的答案以用 2 个代码示例进行解释。我只是再提供一个参考来理解。相反,我会更新对主要Selenium 网站的引用。
【解决方案2】:

你想等5s。你可以试试:

  1. Thread.sleep(5000);
  2. (new WebDriverWait(driver, 5)) .until(new ExpectedCondition<WebElement>()

【讨论】:

    猜你喜欢
    • 2021-03-25
    • 2011-01-14
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多