在Java Web项目上运行时,Selenium会抛出“NoSuchMethodError”

时间:2017-08-30 16:46:47

标签: java maven selenium java-ee nosuchmethoderror

我已经用Java中的Selenium库编写了一些逻辑来做一些简单的Web抓取(逻辑对于这个问题并不重要),当我从Java Web项目中的一个简单的测试文件的main()函数运行时。当我尝试移动这个逻辑代码甚至只是从我的实际servlet或JSP文件中调用此代码时,服务器崩溃时会出现以下异常:

java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;)V
at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:136)
at org.openqa.selenium.firefox.GeckoDriverService.access$000(GeckoDriverService.java:41)
at org.openqa.selenium.firefox.GeckoDriverService$Builder.usingFirefoxBinary(GeckoDriverService.java:108)
at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:204)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:103)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:99)
at scraping.OneCardScraper.<init>(OneCardScraper.java:41)
at org.apache.jsp.Home_jsp._jspService(Home_jsp.java:212)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)

错误指向此行:driver = new FirefoxDriver();

我在这里做错了什么?

我很困惑为什么Selenium依赖项可能在Web平台上遇到麻烦而不是典型的测试运行,因为依赖项应该继续存在。

我尝试过:

  • 多次重启IDE
  • 清洁,建筑等
  • 将整个项目迁移到基于Maven的依赖系统
  • 更改selenium中的浏览器类型(PhantomJS,HTMLUnit,Chrome,FireFox)
  • 将Webdriver位置变量添加到web.xml上下文参数

我正在使用:

  • Netbeans 8.2
  • Java EE 7
  • Selenium 3.5.3

2 个答案:

答案 0 :(得分:1)

这通常是您的类路径/依赖项的问题。 NoSuchMethodError意味着在运行时加载的类不包含在编译时可用的方法。

检查classpath以获得所有selenium模块的正确版本以及依赖关系。并检查重复。我的猜测是你的类路径中有错误的google guava版本。

答案 1 :(得分:1)

这基本上意味着Guava依赖的冲突。

您可能想要查看此Selenium commit和这些Guava 21 release notes。最新的Selenium版本需要Guava 21+,而Guava 21+需要Java 8.因此您必须降级Selenium或升级Java以避免此类问题。