java.lang.NoSuchMethodError:使用 ChromeDriver 和 Selenium 的“java.util.stream.Collector com.google.common.collect.ImmutableList.toIm

2024-01-08

我正在研究 Selenium,我有这个测试方法:

@Test
public void testHeight() {
    System.setProperty("webdriver.chrome.driver", "Drivers/chromedriver.exe");
    
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.rapidtables.com/convert/length/feet-to-cm.html");
    
    driver.findElement(By.xpath("//*[@id=\"x\"]")).sendKeys("6");
    driver.findElement(By.xpath("//*[@id=\"calc\"]")).click();
    
    WebElement txtBoxContent = driver.findElement(By.xpath("//*[@id=\"doc\"]/form/table/tbody/tr[3]/td[2]/input"));
    
    String result = txtBoxContent.getAttribute("value");
    
    driver.close();
    
    System.out.println("The value is: " + result);
    
    assertEquals((double)180,Double.valueOf(result), 5);
    
}

当我尝试在我的项目中使用 Junit 运行 Selenium 时,我收到此错误。 奇怪的是,我尝试在一个只有该方法的新项目中运行该方法,并且一切正常。我没有忘记任何事情,Drivers/chromedriver.exe 包含在我的项目中,所有 Selenium 驱动程序都添加到类路径中。

java.lang.NoSuchMethodError: 'java.util.stream.Collector com.google.common.collect.ImmutableList.toImmutableList()'
    at org.openqa.selenium.chrome.ChromeOptions.asMap(ChromeOptions.java:292)
    at org.openqa.selenium.remote.NewSessionPayload.create(NewSessionPayload.java:94)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:68)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:213)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
    at test.selenium.TestAttractionViewSortByDistance.testHeight(TestAttractionViewSortByDistance.java:18)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:43)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
    at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
    at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:82)
    at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:73)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:141)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)

EDIT:

问题是 selenium 库也需要添加到 WebContent/lib 中。


这个错误信息...

java.lang.NoSuchMethodError: 'java.util.stream.Collector com.google.common.collect.ImmutableList.toImmutableList()'

...意味着有一些不兼容您正在使用的二进制文件的版本之间。

The Selenium https://stackoverflow.com/questions/54459701/what-is-selenium-and-what-is-webdriver/54482491#54482491您在其中看到此错误的 Junit 项目的版本不匹配guava https://stackoverflow.com/questions/57634492/org-openqa-selenium-remote-service-driverservicebuilder-createargslcom-google/57658406#57658406安装。


Solution

您需要配置兼容版本guava https://stackoverflow.com/questions/51856558/java-lang-nosuchmethoderror-com-google-common-base-preconditions-checkargument/51872398#51872398根据下面的列表:

  • Selenium v2.49.0:将 guava 升级到版本 19
  • Selenium v3.1.0:需要更新到最新的番石榴版本21.0
  • Selenium v3.5.0:将 guava 升级到版本 22。
  • Selenium v3.5.1:将 guava 升级到版本 23。
  • Selenium v3.12.0: 番石榴-23.6-jre
  • Selenium v3.13.0:番石榴-25.0-jre
  • Selenium v3.14.0:番石榴-25.0-jre
  • Selenium v3.141.0:番石榴-25.0-jre
  • Selenium v3.141.5:番石榴-25.0-jre
  • Selenium v3.141.59:番石榴-25.0-jre

参考

您可以在以下位置找到一些相关的详细讨论:

  • 不兼容的库版本selenium / guava https://stackoverflow.com/questions/50284776/incompatible-library-version-selenium-guava/50287063#50287063
  • 线程“main”中的异常 java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;) https://stackoverflow.com/questions/52014053/exception-in-thread-main-java-lang-nosuchmethoderror-com-google-common-base-p/52034052#52034052
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java.lang.NoSuchMethodError:使用 ChromeDriver 和 Selenium 的“java.util.stream.Collector com.google.common.collect.ImmutableList.toIm 的相关文章

随机推荐