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

查看:18
本文介绍了java.lang.NoSuchMethodError: org.openqa.selenium.os.CommandLine.find(Ljava/lang/String;)Ljava/lang/String;在将 PhantomJS 2.1.1 与 Selenium 一起使用时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

操作系统 - Windows 7

OS - Windows 7

PhantomJS 版本 - 2.1.1

PhantomJS version - 2.1.1

Selenium - 3.8.1(硒服务器).

Selenium - 3.8.1(selenium-server).

JDK - 152.

我正在尝试使用 PhantomJS 运行简单的测试:

I'm trying to run simple test, using PhantomJS:

1) 初始化驱动:

System.setProperty("phantomjs.binary.path","src\main\resources\phantomjs.exe");
WebDriver driver = new PhantomJSDriver();

2) 任何测试,让它验证 en.wikipedia.org 上的欢迎"文本:

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)

谷歌搜索显示,此类问题时有发生(不兼容的 selenium/PhantomJS).问题:是否有任何解决方法可以让最后一个 selenium 和 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 工具来清除所有操作系统系统的琐事.
  • 系统重启
  • 仅添加 Selenium-Java Clients 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\	ophantomjs-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/");
    

  • 执行您的测试

    这篇关于java.lang.NoSuchMethodError: org.openqa.selenium.os.CommandLine.find(Ljava/lang/String;)Ljava/lang/String;在将 PhantomJS 2.1.1 与 Selenium 一起使用时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆