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 milis。您已经从文档中提到过,也可以从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 以秒为单位进行评估,轮询以毫秒为单位。

要可视化...

尝试 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 milis -> 0 秒,1500 milis -> 1 秒(并记录到控制台。

希望这能清除!!

最新更新