【问题标题】:How to use built-in ExpectedConditions with FluentWait?如何在 FluentWait 中使用内置的 ExpectedConditions?
【发布时间】:2018-04-06 20:12:08
【问题描述】:

在 Selenium (Java) 中,我想将 ExpectedConditions 与 FluentWait 一起使用。我正在尝试以下不起作用的代码。它不会等待元素出现在 DOM 中。

有人可以帮忙吗?

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                    .withTimeout(10, TimeUnit.SECONDS)
                    .pollingEvery(1, TimeUnit.SECONDS);

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("mybutton")));

注意:我已经用 WebDriverWait 试过这个,它是工作文件。我正在尝试使用 FluentWait,因为我想控制轮询超时。

【问题讨论】:

  • 你是否同时使用流利和显式等待?
  • 只试Wait&lt;WebDriver&gt; wait = new FluentWait&lt;WebDriver&gt;(driver) .withTimeout(10, TimeUnit.SECONDS) .pollingEvery(1, TimeUnit.SECONDS); wait.until(ExpectedConditions.presenceOfElementLocated(By.id("mybutton")));
  • 感谢您的回答。其实是我的错。请忽略第二个陈述。我已删除该问题并编辑了问题。我使用正确。这行不通。这不遵守 10 秒的超时。电话立即响起。

标签: selenium selenium-webdriver webdriver fluentwait


【解决方案1】:

一点背景:

流畅的等待

Fluent WaitWait 接口的实现,用户可以动态配置其超时和轮询间隔。 FluentWait 实例定义了等待条件的最长时间以及检查条件的频率。用户还可以将等待配置为在等待时忽略特定类型的异常,例如在页面上搜索元素时NoSuchElementExceptions

WebDriverWait

WebDriverWait 是使用 WebDriver 实例的 FluentWait 的定制版本。

您可以在这两个 QA Implicit vs Explicit vs Fluent WaitDifferences between impilicit, explicit and fluentwait 中找到关于 WebDriverWaitFluentWait 的详细讨论。

预期条件

ExpectedConditions 是定制的固定条件,通常在 webdriver 测试中很有用。


根据您的问题,因为您是 trying with FluentWait since you want to control polling timeout,您仍然可以通过 WebDriverWait 达到同样的效果,如下所示:

  • WebDriverWait 有 3 个 构造函数,其中之一是:

    WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis)
    
  • 详情:

    public WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis)
    
    This wait will ignore instances of NotFoundException that are encountered by default in the `until` condition, and immediately propagate all others. You can also add more to the ignore list by calling ignoring(exceptions to add).
    
    Parameters:
    driver - The WebDriver instance to pass to the expected conditions
    timeOutInSeconds - The timeout in seconds when an expectation is called
    sleepInMillis - The duration in milliseconds to sleep between polls (polling interval).
    

解决办法:

您可以使用上述WebDriverWait构造函数,仍然可以控制轮询间隔。

注意:为了让您的程序逻辑简单易懂,请使用 WebDriverWait 而不是 Fluent Wait,除非绝对必要。

琐事:

想进一步了解Fluent Wait可以关注讨论Selenium Webdriver 3.0.1-[Eclipse-Java-Chrome]: Selenium showing error for FluentWait Class

【讨论】:

  • 如果我使用 WebDriver 等待,我将无法使用预设条件。我需要使用它们来进行轮询。
  • 查看我在回答中提供的构造函数以及指向 JavaDoc 的链接。你也可以做到。请参阅我链接的讨论。如果您需要进一步的帮助,请告诉我。
  • 知道了,谢谢。我的错。构造函数已经在 webdriver 等待中。因此,应该始终将 ExpectedConditions 与 WebDriverWait 一起使用,而不是 FluentWait 类。
【解决方案2】:

是的,NarendraR 说的是正确的。当您为 FluentWait 创建对象时,使用相同的对象来写入 ExpectedConditions。

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(10, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS);
wait.unitl(ExpectedConditions.presenceOfElementLocated(By.id("mybutton")));

【讨论】:

  • 第二行有错别字。它必须阅读wait.until(ExpectedConditions.presenceOfElementLocated(By.id("mybutton")));
猜你喜欢
  • 2013-11-01
  • 2021-04-25
  • 2019-09-20
  • 2020-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多