java.lang.NoSuchMethodError:org.openqa.selenium.os.CommandLine.find(Ljava/lang/String;)Ljava/lang/String;在将PhantomJS 2.1.1与Selenium一起使用时 [英] java.lang.NoSuchMethodError: org.openqa.selenium.os.CommandLine.find(Ljava/lang/String;)Ljava/lang/String; while using PhantomJS 2.1.1 with Selenium
问题描述
OS-Windows 7
OS - Windows 7
PhantomJS版本-2.1.1
PhantomJS version - 2.1.1
硒-3.8.1(硒服务器).
Selenium - 3.8.1(selenium-server).
JDK-152.
我正在尝试使用PhantomJS运行简单的测试:
I'm trying to run simple test, using PhantomJS:
1)初始化驱动程序:
1) initializing the driver:
System.setProperty("phantomjs.binary.path","src\\main\\resources\\phantomjs.exe");
WebDriver driver = new PhantomJSDriver();
2)进行任何测试,让它在en.wikipedia.org上验证文本"welcome":
2) any test, let it be verifying text "welcome" on en.wikipedia.org:
driver.get("http://en.wikipedia.org");
System.out.println(driver.findElement(By.xpath("//div[contains(text(),'Welcome')]")).isDisplayed());
3)运行测试,但收到错误:
3) Running test, but receiving errors:
Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.os.CommandLine.find(Ljava/lang/String;)Ljava/lang/String;
at org.openqa.selenium.phantomjs.PhantomJSDriverService.findPhantomJS(PhantomJSDriverService.java:232)
at org.openqa.selenium.phantomjs.PhantomJSDriverService.createDefaultService(PhantomJSDriverService.java:181)
at org.openqa.selenium.phantomjs.PhantomJSDriver.<init>(PhantomJSDriver.java:104)
at org.openqa.selenium.phantomjs.PhantomJSDriver.<init>(PhantomJSDriver.java:94)
Google搜索显示,此类问题有时会发生(不兼容selenium/PhantomJS). 问:结交最后一个硒和2.1.1 PhantomJS的好朋友有什么解决方法吗?
Googling showed, that such troubles occur time to time (non-compatible selenium/PhantomJS). Question: is there any workaround for making last selenium(s) and 2.1.1 PhantomJS good friends?
注意:任何其他驱动程序都可以正常工作(edge,chrome,ff).
note: any other driver works fine (edge, chrome, ff).
推荐答案
您所看到的错误说明了一切:
The error you are seeing says it all :
NoSuchMethodError: org.openqa.selenium.os.CommandLine.find(Ljava/lang/String;)Ljava/lang/String;
班级 NoSuchMethodError
NoSuchMethodError
> 扩展了 IncompatibleClassChangeError
,并且根据 Java Docs ,如果应用程序尝试调用类(静态或实例)的指定方法,并且该类不再具有该方法的定义.通常,此错误由编译器捕获,并且只有在类的定义发生不兼容更改的情况下,此错误才可能在运行时发生.
Class NoSuchMethodError
NoSuchMethodError
extends IncompatibleClassChangeError
and as per the Java Docs it is thrown if an application tries to call a specified method of a class (either static or instance), and that class no longer has a definition of that method. Normally, this error is caught by the compiler and this error can only occur at run time if the definition of a class has incompatibly changed.
执行以下步骤:
- 将您的
JDK
更新到最新版本(Java 8 Update 151
) - 从IDE中清理
Project Space
. - 运行
CCleaner
工具以清除所有OS系统琐事. - 接受
System Reboot
- 仅添加 Selenium-Java客户端v3.8.1 jar.
-
使用 PhantomJSDriver(GhostDriver)时,您需要添加以下 Maven依赖项:
- Update your
JDK
to the most recent versions (Java 8 Update 151
) - Clean up the
Project Space
from the IDE. - Run
CCleaner
tool to wipe off all the OS system chores. - Take a
System Reboot
- Add only Selenium-Java Clients v3.8.1 jar.
As you are using PhantomJSDriver (GhostDriver) you need to add the following Maven Dependency :
<dependency>
<groupId>com.github.detro</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>1.4.0</version>
</dependency>
您需要使用 phantomjs
二进制文件的绝对路径更新 System.setProperty
行,如下所示:
You need to update the line System.setProperty
with the absolute path of the phantomjs
binary as follows :
File path=new File("C:\\path\\\to\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("phantomjs.binary.path",path.getAbsolutePath());
WebDriver driver= new PhantomJSDriver();
driver.navigate().to("https://www.google.co.in/");
执行您的 Test
Execute your Test
这篇关于java.lang.NoSuchMethodError:org.openqa.selenium.os.CommandLine.find(Ljava/lang/String;)Ljava/lang/String;在将PhantomJS 2.1.1与Selenium一起使用时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!