wait.until(ExpectedCondition)生成错误

时间:2017-12-20 08:50:31

标签: selenium selenium-webdriver wait

升级到selenium Java 3.8.1后,wait.until(ExpectedCondition)已开始提供错误消息。

以下代码

WebElement framei = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='ctl00_ContentPlaceHolder1_dlgModal_IFrame']")));       
    driver.switchTo().frame(framei);
    WebElement AcceptRadioButton=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='RblStatus']/tbody/tr[1]/td/label")));
    AcceptRadioButton.click();

给出以下错误:

Type The method until(Function<? super WebDriver,V>) in the type FluentWait<WebDriver> is not applicable for the arguments (ExpectedCondition<WebElement>)

我尝试通过删除

的Selenium java版本3.8.1来解决以下问题

3 个答案:

答案 0 :(得分:1)

与你相同的问题,但不太确定Eugene S答案我在selenium-java 2.53.1和3.8.1的源代码中搜索,看看FluentWait类之间有什么不同。这是直到不同版本的功能:

2.53.1:

  public <V> V until(Function<? super T, V> isTrue) {
    long end = clock.laterBy(timeout.in(MILLISECONDS));
    Throwable lastException = null;
    while (true) {
      try {
        V value = isTrue.apply(input);
        if (value != null && Boolean.class.equals(value.getClass())) {
          if (Boolean.TRUE.equals(value)) {
            return value;
          }
        } else if (value != null) {
          return value;
        }
      } catch (Throwable e) {
        lastException = propagateIfNotIgnored(e);
      }

  // Check the timeout after evaluating the function to ensure conditions
  // with a zero timeout can succeed.
      if (!clock.isNowBefore(end)) {
        String message = messageSupplier != null ?
            messageSupplier.get() : null;

        String toAppend = message == null ?
            " waiting for " + isTrue.toString() : ": " + message;

        String timeoutMessage = String.format("Timed out after %d seconds%s",
            timeout.in(SECONDS), toAppend);
        throw timeoutException(timeoutMessage, lastException);
      }

      try {
        sleeper.sleep(interval);
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new WebDriverException(e);
      }
    }
  }

OR

  public <V> V until(Function<? super T, V> isTrue) {
    long end = clock.laterBy(timeout.in(MILLISECONDS));
    Throwable lastException;
    while (true) {
      try {
        V value = isTrue.apply(input);
        if (value != null && (Boolean.class != value.getClass() || Boolean.TRUE.equals(value))) {
          return value;
        }

    // Clear the last exception; if another retry or timeout exception would
    // be caused by a false or null value, the last exception is not the
    // cause of the timeout.
        lastException = null;
      } catch (Throwable e) {
        lastException = propagateIfNotIgnored(e);
      }

  // Check the timeout after evaluating the function to ensure conditions
  // with a zero timeout can succeed.
      if (!clock.isNowBefore(end)) {
        String message = messageSupplier != null ?
            messageSupplier.get() : null;

        String timeoutMessage = String.format(
            "Expected condition failed: %s (tried for %d second(s) with %s interval)",
            message == null ? "waiting for " + isTrue : message,
            timeout.in(SECONDS), interval);
        throw timeoutException(timeoutMessage, lastException);
      }

      try {
        sleeper.sleep(interval);
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new WebDriverException(e);
      }
    }
  }

AND IN 3.8.1:

gtag

我没有看到三个函数参数之间有任何区别,但是我工作的项目没有给我带来2.53.1版本的任何错误,但是3.8.1我有与Akhil相同的错误。

答案 1 :(得分:0)

正如错误消息中所述:

FluentWait<WebDriver> is not applicable for the arguments (ExpectedCondition<WebElement>)

从Selenium 3开始,until方法声明现在看起来像这样:

public <V> V until(Function<? super T, V> isTrue) 

其中Function是:

public interface Function<T, R>

因此它已被转换为使用Java 8功能接口。您需要相应地重写您的预期条件。

答案 2 :(得分:0)

根据最佳做法,我们必须尝试使用​​适当的<iframe>切换为WebDriverWait,如下所示:

new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@id='ctl00_ContentPlaceHolder1_dlgModal_IFrame']")));