gpt4 book ai didi

selenium - Fluent Wait 不起作用-Selenium Webdriver

转载 作者:行者123 更新时间:2023-12-02 21:15:20 27 4
gpt4 key购买 nike

网页中不存在下面的 WebElement。不过我想了解流畅的等待。这里,当 webdriver 尝试识别 BE_flight_flseah_btn 时,根据代码,它应该等待最多 5 分钟,但它会在几秒钟内抛出错误,表示没有这样的元素。

Wait<WebDriver> w=new FluentWait<WebDriver>(dr)
.withTimeout(5,TimeUnit.MINUTES)
.pollingEvery(5,TimeUnit.MILLISECONDS)
.ignoring(NoSuchElementException.class);
WebElement we=w.until(new Function<WebDriver,WebElement>()
{
public WebElement apply(WebDriver dr)
{
return dr.findElement(By.id("BE_flight_flseah_btn"));
}
});

为什么没有等待 5 分钟,这段代码有什么问题吗?请帮我解决这个问题。

最佳答案

确保您已导入正确的 NoSuchElementException 类。

import org.openqa.selenium.NoSuchElementException;

因为如果您使用 eclipse 或 Netbeans 等任何 IDE,自动导入很可能会导入 import java.util.NoSuchElementException; 这是一个错误的

您每 5 毫秒轮询一次并等待 5 分钟。所以它会检查60000次才能找到该元素。这会影响性能。要么将轮询TimeUnit增加到秒,要么将超时减少到秒

流畅等待:假设您有一个元素,有时仅 1 秒内出现,有时需要几分钟才能出现。在这种情况下,最好使用流畅等待,因为这将尝试一次又一次地查找元素,直到找到它或直到最终计时器耗尽。

显式等待:有时可能会出现特定元素加载时间超过一分钟的情况。在这种情况下,您绝对不喜欢为隐式等待设置很长的时间,因为如果您这样做,您的浏览器将为每个元素等待相同的时间。

为了避免这种情况,您只需在所需的元素上放置单独的时间即可。通过遵循这一点,您的浏览器隐式等待时间对于每个元素来说都会很短,对于特定元素来说会很长。

根据您的要求,您可以使用 WebDriverWait 而不是 FluentWait 和 Wait以及ExpectedConditions虽然 WebDriverWait 是 Fl​​uentWait 的扩展。

您可以阅读有关 comparison over this link 的信息

您的情况可以处理为

//timeunit in seconds.
//There are two other constructors available you can see that in the links

WebDriverWait wait = new WebDriverWait(driver, 300);

WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("BE_flight_flseah_btn")));

关于selenium - Fluent Wait 不起作用-Selenium Webdriver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31097735/

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