【问题标题】:Method until in class FluentWait<T> cannot be applied to given types类 FluentWait<T> 中的方法 until 不能应用于给定类型
【发布时间】:2020-01-17 17:20:47
【问题描述】:

我在使用 java 和 gradle 版本 3.4 编译 selenium 自动化应用程序时遇到以下错误。

错误:

  method until in class FluentWait<T> cannot be applied to given types;
        return getWebDriverWait().until(ExpectedConditions.elementToBeClickable(GOTO_ICON));
                                 ^
  required: Function<? super WebDriver,V>
  found: ExpectedCondition<WebElement>
  reason: cannot infer type-variable(s) V
    (argument mismatch; ExpectedCondition<WebElement> cannot be converted to Function<? super WebDriver,V>)
  where V,T are type-variables:
    V extends Object declared in method <V>until(Function<? super T,V>)
    T extends Object declared in class FluentWait

构建.gradle

   compile 'io.appium:java-client:3.1.0'
   compile 'com.applitools:eyes-selenium-java-jersey1x:2.29'

   compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '2.46.0'
   compile 'org.seleniumhq.selenium:selenium-firefox-driver:2.52.0'
   compile 'org.seleniumhq.selenium:selenium-ie-driver:2.52.0'

源代码:

By GOTO_ICON = By.id("GoTo");
String windowContentLoaded = "//*[@id=\"windowContentLoaded\"]";

public WebElement getGoToHeader() {
    waitForPageToBeLoaded();
    return getWebDriverWait().until(ExpectedConditions.elementToBeClickable(GOTO_ICON));
}

public void waitForPageToBeLoaded() {
    sleepSeconds(3);
    getWebDriverEx().waitForInvisibleElement(By.xpath(windowContentLoaded));
    return;
}

【问题讨论】:

  • 假设您正在尝试编译某种工作开源,我的猜测是:您的 Java 版本是错误的。您应该使用 1.8 但使用 1.7 ;反之亦然。
  • 运气不好。在我使用 java 1.8 之前。现在切换到 1.7 并遇到了许多问题。
  • 你的 GOTO_ICON 定位器是什么?
  • 我不确定我是否遵循,在某些时候这段代码可以正常工作,但现在不行了吗?根据您提供的代码,我猜您正在尝试直接从 webdriver 调用 .until(),但是您应该将 webdriver 作为参数传递给 fluentwait 构造函数。
  • @kushalツ GOTO_ICON = By.id("GoTo");

标签: java selenium-webdriver gradle ui-automation


【解决方案1】:

不确定 Selenium 2.46.0 中错误的原因是什么,但是从 3.x 版本切换到版本时,我遇到了完全相同的错误4.x

它可能无法帮助 OP 解决问题,但是我正在为搜索相同异常的人发布解决方案。

在最近版本的 Selenium 框架中,getWebDriverWait().until() 接口发生了一些变化:

  • 旧版本

  • 新版本

现在它接受函数式接口,带有一个参数 (WebDriver) 和返回值取决于 WebDriverWait 实现,在大多数情况下是 ExpectedConditions

尝试将您的代码更改为:

public WebElement getGoToHeader() {
    waitForPageToBeLoaded();
    return getWebDriverWait().until(
        webDriver -> ExpectedConditions.elementToBeClickable(GOTO_ICON).apply(webDriver)
    );
}

【讨论】:

  • 这非常有用。我希望我可以多次投票。
猜你喜欢
  • 2018-07-18
  • 2019-01-17
  • 1970-01-01
  • 2012-10-21
  • 2019-03-22
  • 1970-01-01
  • 1970-01-01
  • 2018-08-15
相关资源
最近更新 更多