java - FluentWait 默认超时为零
问题描述
当我们没有像下面这样指定时,FluentWait 的默认超时是多少,但我知道默认轮询是 500 毫秒。
FluentWait<WebDriver> wait=new FluentWait<WebDriver>(dr)
//.withTimeout(Duration.ofSeconds(20))
//.pollingEvery(Duration.ofSeconds(2))
.ignoring(NoSuchElementException.class);
在超时异常中,我看到-
Exception in thread "main" org.openqa.selenium.TimeoutException:(tried for 0 second(s) with 500 milliseconds interval)
这是否意味着我们的默认最大超时为 0 秒。当我浏览文档时- 它说 DEFAULT_SLEEP_TIMEOUT 为500L
解决方案
每个 FluentWait 实例定义等待条件的最长时间,默认为 500 英里。您已经从文档中提到过,也可以从FluentWait.java确认相同
protected static final long DEFAULT_SLEEP_TIMEOUT = 500;
private static final Duration DEFAULT_WAIT_DURATION = Duration.ofMillis(DEFAULT_SLEEP_TIMEOUT);
超时异常日志Exception in thread "main" org.openqa.selenium.TimeoutException:(tried for 0 second(s) with 500 milliseconds interval)
,因为这是在控制台中记录异常的方式,即 withTimeout 以秒为单位进行评估,而 pollingEvery 以毫秒为单位。
要形象化...
尝试1:
wait=new FluentWait<>(driver)
.withTimeout(Duration.ofMillis(500))
.pollingEvery(Duration.ofSeconds(2))
.ignoring(NoSuchElementException.class);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//someElementLocator")));
例外
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //someElementLocator (tried for 0 second(s) with 2000 milliseconds interval)
尝试2:
wait=new FluentWait<>(driver)
.withTimeout(Duration.ofMillis(1500))
.pollingEvery(Duration.ofSeconds(2))
.ignoring(NoSuchElementException.class);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//someElementLocator")));
例外
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //someElementLocator (tried for 1 second(s) with 2000 milliseconds interval)
这确认了评估等待超时和轮询间隔的方式,然后可能四舍五入到地板(500 英里 -> 0 秒,1500 英里 -> 1 秒)并记录到控制台。
希望这清除!
推荐阅读
- kubernetes - Kubernetes 服务部署在默认命名空间而不是使用 Helm 定义的命名空间
- c# - 即使在设置超时并且程序等待设置的持续时间之后,WebRequest.GetResponse() 也会引发超时异常
- c# - 一次只允许一个用户访问操作
- javascript - 为什么 React 会跳过正确设置为 state 的最后一个问题?
- javascript - 如何在reactjs中按日期排列图像?
- jquery - Json 序列化不是使用 Jquery 序列化一些 html 数据
- java - 如何在内部函数中使用类变量?
- linux-device-driver - 从嵌入式板上的驱动程序控制 USART RTS 引脚
- c++ - ComboBox - OnCtlColor:如何为整个列表着色
- c# - 使用 2 个本地数据库启用数据库迁移