在Selenium(Java)中,我想在FluentWait中使用ExpectedConditions。我正在尝试使用不起作用的代码。它不是在等待元素出现在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,因为我想控制轮询超时。
答案 0 :(得分:2)
一些背景知识:
Fluent Wait
是Wait
接口的实现,用户可以动态配置其超时和轮询间隔。 FluentWait
实例定义了等待条件的最长时间以及检查条件的频率。用户还可以配置等待以在等待时忽略特定类型的异常,例如在页面上搜索元素时NoSuchElementExceptions
。
WebDriverWait
是使用WebDriver实例的FluentWait的量身定制版本。
您可以在这两个质量检查Implicit vs Explicit vs Fluent Wait
和{{3}中找到有关 WebDriverWait
和 FluentWait
的详细讨论}。
Differences between impilicit, explicit and fluentwait
是定制的固定条件,通常在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
,您可以按照ExpectedConditions
答案 1 :(得分:0)
是的,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")));