启动无头浏览器时出错java.lang.NoSuchMethodError错误

时间:2017-05-29 08:21:50

标签: selenium selenium-webdriver phantomjs

使用 Phantomjs 启动无头浏览器时,获取以下异常

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)

操作系统是Mac

启动无头浏览器的配置是

File src = new File("src/test/resources/phantomjs_mac");
System.setProperty("phantomjs.binary.path", src.getAbsolutePath());
driver = new PhantomJSDriver();

更新了完整的跟踪

FAILED CONFIGURATION: @BeforeMethod LaunchBrowser
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)
    at baseHelper.BrowserLauncher.LaunchBrowser(BrowserLauncher.java:108)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:589)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:782)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
    at org.testng.TestNG.run(TestNG.java:1064)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)

2 个答案:

答案 0 :(得分:0)

以下是您的问题的答案:

当我们使用Phantomjs时,我们必须提供Phantomjs可执行文件的绝对路径。在指定实施 WebDriver

之前,请务必指定new PhantomJSDriver();班级

以下是Windows中的示例:

 File path=new File("C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
 System.setProperty("phantomjs.binary.path",path.getAbsolutePath());
 WebDriver driver= new PhantomJSDriver();
 driver.manage().window().maximize();
 driver.navigate().to("https://www.google.co.in/");

PS:请将其转换为Mac可识别格式。

如果这回答你的问题,请告诉我。

答案 1 :(得分:0)

System.setProperty(“phantomjs.binary.path”,src.getAbsolutePath())在PhanotmJS的情况下不起作用 但是,您可以设置DesiredCapabilities并将其传递给PhantomJSDriver。

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, System.getProperty("user.dir") + /drivers/phantomjs.exe");

WebDriver driver = new PhantomJSDriver(capabilities);
driver.navigate().to("https://www.google.co.in/");
driver.get("http://google.com");

确保您的pom文件不使用selenium 3.6.0版。相反,请使用以下依赖项。

<dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.5.3</version>
</dependency>

另外,WebDriver driver = new PhantomJSDriver();将始终导致错误,因此您不能使用空构造函数。

我希望有所帮助。