2017-05-29 148 views
-1

当启动使用Phantomjs无头的浏览器,得到了下面的异常收到错误java.lang.NoSuchMethodError错误,而开展模拟浏览器

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) 

OS是苹果

配置为启动无头浏览器是

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) 
+0

您可以考虑更新我们关于您尝试使用'Phantomjs'的平台。一旦你初始化'Phantomjs driver'尝试导航到一个url。如果您发现任何错误,请考虑提供完整的堆栈跟踪以供进一步分析。谢谢 – DebanjanB

+0

@DebanjanB Os是mac。正如我想用于无头浏览器,我正在考虑phantomjs。我在将phantomjs驱动程序分配给webdriver变量时出现此错误。要导航到URL,驱动程序变量应该有一个值,但null被分配给phantomjs驱动程序。如果我用户使用Firefox驱动程序,测试运行顺利。 –

+0

您能否确认文件(src)是否存在? – Eugene

回答

0

这里是回答你的问题:

当我们使用Phantomjs我们必须提供Phantomjs可执行的绝对路径。记住指定实施new PhantomJSDriver();

之前指定WebDriver类下面是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的可识别的格式。

让我知道这个答案是否是您的问题。

+0

不,'WebDriver driver = new PhantomJSDriver();'这是赋予驱动器空值 –

+0

它已经转换'File src = new File(“src/test/resources/phantomjs_mac”);'我有kep它在项目目录 –

+0

你可以考虑更新当前的代码块和完整的错误stacktrace吗?谢谢 – DebanjanB

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文件不使用硒版本3.6.0。相反,使用下面的依赖关系。

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

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

我希望有帮助。