【问题标题】:java.lang.NoSuchMethodError: org.openqa.selenium.os.CommandLine.find(Ljava/lang/String;)Ljava/lang/String; while using PhantomJS 2.1.1 with Seleniumjava.lang.NoSuchMethodError: org.openqa.selenium.os.CommandLine.find(Ljava/lang/String;)Ljava/lang/String;在将 PhantomJS 2.1.1 与 Selenium 一起使用时
【发布时间】:2017-12-10 22:05:36
【问题描述】:

操作系统 - Windows 7

PhantomJS 版本 - 2.1.1

Selenium - 3.8.1(硒服务器)。

JDK - 152.

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

1) 初始化驱动:

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

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

driver.get("http://en.wikipedia.org");
System.out.println(driver.findElement(By.xpath("//div[contains(text(),'Welcome')]")).isDisplayed());

3) 运行测试,但收到错误:

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 成为好朋友?

注意:任何其他驱动程序都可以正常工作(edge、chrome、ff)。

【问题讨论】:

  • 您能否使用您的 Selenium/WebDriver/Browser 版本以及您的代码试验更新问题?
  • 问题已更新。

标签: selenium selenium-webdriver webdriver phantomjs


【解决方案1】:

只是为了添加一个可以面对相同异常的不同场景。

在使用 Eclipse 时,下面的代码可以正常工作:

File file = new File("C://phantomjs.exe");
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
WebDriver driver = new PhantomJSDriver();

在使用 Intellij 时,上面的代码抛出了问题中提到的错误。

但以下内容适用于 Intellij:

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:\\phantomjs.exe");

WebDriver driver = new PhantomJSDriver(capabilities);

注意:不要忘记更改exe路径。

【讨论】:

    【解决方案2】:

    您看到的错误说明了一切:

    NoSuchMethodError: org.openqa.selenium.os.CommandLine.find(Ljava/lang/String;)Ljava/lang/String;
    

    NoSuchMethodError

    NoSuchMethodError extends IncompatibleClassChangeError 并且根据 Java Docs 如果应用程序尝试调用类的指定方法(静态或实例)并且该类不再有该方法的定义。通常,编译器会捕获此错误,并且如果类的定义发生了不兼容的更改,则此错误只会在运行时发生。

    解决方案

    执行以下步骤:

    • 将您的 JDK 更新到最新版本 (Java 8 Update 151)
    • 从 IDE 中清除 Project Space
    • 运行 CCleaner 工具来清除所有操作系统系统的琐事。
    • 参加System Reboot
    • 仅添加 Selenium-Java Clients v3.8.1 jar。
    • 当您使用 PhantomJSDriver (GhostDriver) 时,您需要添加以下 Maven 依赖项

      <dependency>
          <groupId>com.github.detro</groupId>
          <artifactId>phantomjsdriver</artifactId>
          <version>1.4.0</version>
      </dependency> 
      
    • 您需要将 System.setProperty 行更新为 phantomjs 二进制文件的绝对路径,如下所示:

      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

    【讨论】:

    • 使用 1.4.0 就像一个魅力,谢谢!请注意:不知道为什么,但在我的机器上,我从 com.github.detro 更新时遇到了麻烦。来自 com.codeborne - 没问题。
    猜你喜欢
    • 1970-01-01
    • 2016-02-07
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多