wait.until(ExpectedCondition)生成错误

问题描述 投票:0回答:3

在升级到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来解决以下问题

selenium selenium-webdriver wait
3个回答
1
投票

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

2.53.1 :

  public void until(final Predicate<T> isTrue) {
    until(new Function<T, Boolean>() {
      public Boolean apply(T input) {
        return isTrue.apply(input);
      }

      public String toString() {
        return isTrue.toString();
      }
    });
  }

要么

  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);
      }
    }
  }

和在3.8.1:

  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);
      }
    }
  }

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


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功能接口。您需要相应地重写您的预期条件。


0
投票

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

new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@id='ctl00_ContentPlaceHolder1_dlgModal_IFrame']")));
© www.soinside.com 2019 - 2024. All rights reserved.