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

标签 java selenium nosuchmethoderror webdriverwait

每当我尝试在项目中使用 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

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

最佳答案

该方法的方法签名记录在: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

This interface is now a legacy type. Use java.util.function.Function (or the appropriate primitive specialization such as ToIntFunction) instead whenever possible. Otherwise, at least reduce explicit dependencies on this type by using lambda expressions or method references instead of classes, leaving your code easier to migrate in the future.

To use an existing function (say, named function) in a context where the other type of function is expected, use the method reference function::apply. A future version of com.google.common.base.Function will be made to extend java.util.function.Function, making conversion code necessary only in one direction. At that time, this interface will be officially discouraged.

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

关于java.lang.NoSuchMethodError : org. openqa.selenium.support.ui.FluentWait.until(Lcom/google/common/base/Function;)Ljava/lang/Object;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51934540/

相关文章:

java 设置变量

angularjs - 如何使用 Protractor ElementArrayFinder 避免 'Index out of Bound' 异常

java - 如何修复java中的NoSuchMethodError

java - 在 javax.ws.rs.core.MultivaluedMap.addAll 处获取 java.lang.NoSuchMethodError

java - 列出具有预定义大小或没有大小的可变参数

java - Intellij 中的 Spring Boot 应用程序 : Debug takes 10x longer than Run

java - org.springframework.beans.NotWritablePropertyException : Invalid property 'xxxx' of bean class :

python - Selenium xpath 元素属性

ruby-on-rails-3 - 试图让 Selenium 在 Rails 3 中工作 - "WebMock::NetConnectNotAllowedError"

java - newInstance() 找不到存在的构造函数