【问题标题】:java.lang.NoSuchMethodError: org.openqa.selenium.support.ui.FluentWait.until(Lcom/google/common/base/Function;)Ljava/lang/Object;java.lang.NoSuchMethodError: org.openqa.selenium.support.ui.FluentWait.until(Lcom/google/common/base/Function;)Ljava/lang/Object;
【发布时间】:2019-01-26 19:22:42
【问题描述】:

每当我尝试在我的项目中使用 wait.until(ExpectedConditions) 时都会遇到以下错误:

java.lang.NoSuchMethodError: org.openqa.selenium.support.ui.FluentWait.until(Lcom/google/common/base/Function;)Ljava/lang/Object;

我看到有问题的示例代码:

public static void clickSearch(WebDriver driver,By by, int timeout){
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
    WebDriverWait wait = new WebDriverWait(driver, 2);
    WebElement webElement = wait.until(ExpectedConditions.elementToBeClickable(by));
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    webElement.click();
}

FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(waitSeconds, TimeUnit.SECONDS).pollingEvery(5, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class);
wait.until(new ExpectedCondition<Boolean>() {
      @Override
      public Boolean apply(WebDriver driver) {
          return ((JavascriptExecutor) driver).executeScript("return document.readyState").toString().equals("complete");
      }
});

我有以下库:

activation.jar
byte-buddy-1.8.3.jar
cglib-nodep-3.2.4.jar
commons-codec-1.10.jar
commons-collections4-4.1.jar
commons-exec-1.3.jar
commons-io-2.5-javadoc.jar
commons-io-2.5.jar
commons-lang3-3.5.jar
commons-logging-1.2.jar
dom4j-1.6.1.jar
dsn.jar
gson-2.8.5.jar
hamcrest-core-1.3.jar
hamcrest-library-1.3.jar
http-client-6.6.0.5.jar
httpclient-4.5.5.jar
httpcore-4.4.9.jar
httpmime-4.5.5.jar
imap.jar
jackson-annotations-2.9.1.jar
jackson-core-2.9.1.jar
jackson-databind-2.9.1.jar
java-client-3.3.0.jar
jaxen-1.1.6.jar
jcl-over-slf4j-1.7.21.jar
jcommander-1.72.jar
jna-4.4.0.jar
json-20151123.jar
jsr305-3.0.2.jar
junit-4.12.jar
jxl-2.6.12.jar
mail.jar
mailapi.jar
netty-3.5.7.Final.jar
ojdbc6.jar
okio-1.15.0.jar
pm-webdriver-6.6.0.5-javadoc.jar
pm-webdriver-6.6.0.5.jar
poi-3.17.jar
poi-examples-3.16.jar
poi-excelant-3.17.jar
poi-ooxml-3.17.jar
poi-ooxml-schemas-3.17.jar
poi-scratchpad-3.17.jar
selenium-3.13\selenium-api-3.13.0.jar
selenium-3.13\selenium-java-3.13.0.jar
selenium-3.13\selenium-remote-driver-3.13.0.jar
selenium-3.13\selenium-server-standalone-3.13.0.jar
selenium-3.13\selenium-support-3.13.0.jar
slf4j-api-1.7.25.jar
smtp.jar
testng-6.14.2.jar
xml-apis-2.9.1.jar
xmlbeans-2.6.0.jar
guava-26.0-jre.jar

我们的项目中不使用 Gradle/Maven。我们将这些库的物理副本存储在一个位置,并使用相同的位置使用 ANT 脚本构建可执行 jar。我们从命令行执行由 ANT 脚本创建的 jar:

java.exe -cp <ProjectJarName>;<Location of jar libraries> <Main class name with whole package>.
For eg : java.exe project.jar;C:\Libraries\*.jar java.main.Main

当我搜索问题时,我看到的常见答案是添加我尝试过的最新番石榴罐,但并没有解决问题。同样在 github 的 selenium 论坛中,我看到人们通过更改 pom.xml 更改顺序来解决问题,我认为我不必这样做,因为我不依赖于 maven。

【问题讨论】:

  • 那么你如何编译你的代码呢?如果使用 IDE,您可以检查 IDE 可以使用哪些版本的库,因为(如果您使用的是),它不会抱怨执行抱怨的未知方法...

标签: java selenium nosuchmethoderror webdriverwait


【解决方案1】:

该方法的方法签名记录在:https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html#until-java.util.function.Function-

你正在传递这个https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedCondition.html,它扩展了com.google.common.base.Function

https://google.github.io/guava/releases/20.0/api/docs/com/google/common/base/Function.html

这个接口现在是一个遗留类型。使用 java.util.function.Function (或适当的原始特化,例如 ToIntFunction) 而是尽可能。否则,至少减少显式 通过使用 lambda 表达式或方法来依赖此类型 引用而不是类,使您的代码更容易迁移 未来。

在上下文中使用现有函数(例如,命名函数) 其他类型的函数是预期的,使用方法参考 功能::应用。 com.google.common.base.Function 的未来版本 将进行扩展java.util.function.Function,进行转换 仅在一个方向上需要的代码。当时这个界面 将被正式劝阻。

这表明您的 JRE 版本或库版本不匹配。当until 的Javadoc 说它的参数应该是java.util.function.Function 时,您收到的错误消息表明您正在传递com.google.common.base.Function

【讨论】:

  • 感谢您分享您的意见。但我看到 com.google.common.base.Function 扩展了 java.util.function.Function。以下是相同的源代码。 IU 正在使用可以进行此更改的 guava 26.0-jre jar。 [github.com/google/guava/blob/master/guava/src/com/google/common/…关于JRE版本,我在1.8.0_131。
  • 由于您使用的是 JRE 1.8+,我链接的 javadoc 中的注释和您发布的来源有帮助吗? This interface is now a legacy type. Use {@code java.util.function.Function} (or the appropriate primitive specialization such as {@code ToIntFunction}) instead whenever possible. 您尝试调用的方法需要 java.util.function.Function,您传递的类说它是旧的并直接使用 java.util.function.Function。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-01
  • 1970-01-01
  • 2014-12-26
  • 1970-01-01
相关资源
最近更新 更多