gpt4 book ai didi

java - 我想使用 Fluent wait 根据元素存在情况返回 true 或 false,该怎么做?

转载 作者:太空宇宙 更新时间:2023-11-04 09:55:06 29 4
gpt4 key购买 nike

我正在使用 Fluent wait,我看到函数的返回值是 WebElement。但是,我想根据元素的存在来重新调整 true 或 false。我该怎么做?我引用此页面 - https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html

代码片段在这里 -

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);

WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("foo"));
}
});

我尝试更改为以下内容,但它给了我错误 -

The method until(Function) in the type Wait is not applicable for the arguments (new Function(){})

这是我改变的 -

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(retryCount, TimeUnit.SECONDS)
.pollingEvery(1, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);

Boolean foo = wait.until(new Function<WebElement, Boolean>() {
public Boolean apply(WebElement by) {
return true;
}
});

我使用的是 Guava 版本 23.0、Selenium 3.0、Java 1.8。*

最佳答案

如果仅元素可见性很重要,则尝试以下操作:-

FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);
wait.withTimeout(Duration.ofSeconds(20));
wait.pollingEvery(Duration.ofSeconds(5));
wait.ignoring(NoSuchElementException.class);

boolean status = wait.until(new Function<WebDriver, Boolean>() {
public Boolean apply(WebDriver driver) {
return driver.findElement(By.name("q")).isDisplayed();
}
});

关于java - 我想使用 Fluent wait 根据元素存在情况返回 true 或 false,该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54247695/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com