【问题标题】:wait.until(ExpectedCondition) generating errorwait.until(ExpectedCondition) 生成错误
【发布时间】:2017-12-20 08:50:30
【问题描述】:

升级到 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


    【解决方案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 相同。

    【讨论】:

      【解决方案2】:

      正如错误消息中所说:

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

      【讨论】:

        【解决方案3】:

        根据最佳实践,我们必须尝试使用​​正确的WebDriverWait 切换到&lt;iframe&gt;,如下所示:

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

        【讨论】:

          猜你喜欢
          • 2012-06-04
          • 2015-03-12
          • 2018-12-05
          • 2014-05-23
          • 2012-09-28
          • 2019-02-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多