首页 > 解决方案 > 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

标签: javaselenium

解决方案


每个 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 秒)并记录到控制台。

希望这清除!


推荐阅读