如何使用Phantomjs在Selenium中打开一个网站

3
我正在尝试使用PhantomJs的无头Webkit通过selenium webdriver打开google.com,但当我执行代码时,系统会抛出错误。phantomJs.exe位于E目录中。请帮助我解决这个问题。
     public static void main(String[] args) throws Exception {

                DesiredCapabilities caps = new DesiredCapabilities();
                caps.setJavascriptEnabled(true);  
   caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "E:\\phantomjs.exe");
                WebDriver driver = new PhantomJSDriver();              
                driver.get("http://www.google.com");

            }
错误:

主线程异常:java.lang.IllegalStateException:phantomjs.binary.path能力/系统属性/PATH变量必须设置驱动程序可执行文件的路径。有关更多信息,请参见https://github.com/ariya/phantomjs/wiki。最新版本可从http://phantomjs.org/download.html下载。 在com.google.common.base.Preconditions.checkState(Preconditions.java:197) 在org.openqa.selenium.phantomjs.PhantomJSDriverService.findPhantomJS(PhantomJSDriverService.java:236) 在org.openqa.selenium.phantomjs.PhantomJSDriverService.createDefaultService(PhantomJSDriverService.java:181) 在org.openqa.selenium.phantomjs.PhantomJSDriver.(PhantomJSDriver.java:104) 在org.openqa.selenium.phantomjs.PhantomJSDriver.(PhantomJSDriver.java:94) 在multidrivers.main(multidrivers.java:35)


1
能力去哪了? - Artjom B.
我不太明白你的意思。有什么我漏掉了吗? - user2044296
@ArtjomB. 意味着你是否已经将你的能力添加到驱动程序中? - Manu
@Manu 显然。你为什么要定义能力,却从未使用它们呢?只需将它们传递给驱动程序即可。文档:http://cdn.ivandemarino.me/phantomjsdriver-javadoc/org/openqa/selenium/phantomjs/PhantomJSDriver.html#PhantomJSDriver%28org.openqa.selenium.Capabilities%29 - Artjom B.
你对于"The path to the driver executable must be set by the phantomjs.binary.path"和"for more information, see https://github.com/ariya/phantomjs/wiki "这两部分有什么问题吗? - SiKing
如果有人能够纠正我上面提到的代码并使其正常工作,那就更好了。 - user2044296
1个回答

6

主线程中的异常:"main" java.lang.IllegalStateException:必须通过phantomjs.binary.path功能/系统属性/PATH变量设置驱动程序可执行文件的路径;

上述问题是由于驱动程序未使用DesiredCapabilities对象进行初始化导致的:

WebDriver driver = new PhantomJSDriver();      

以下更新代码应该可以解决您的问题:
WebDriver driver = new PhantomJSDriver(caps);  

如果您有任何疑问,请告诉我。


谢谢你的帮助,但是当我包含了这个改变后,我得到了以下错误。 “主线程中的异常” java.lang.NoClassDefFoundError: org/openqa/selenium/browserlaunchers/Proxies at org.openqa.selenium.phantomjs.PhantomJSDriverService.createDefaultService(PhantomJSDriverService.java:178) at org.openqa.selenium.phantomjs.PhantomJSDriver.<init>(PhantomJSDriver.java:99) at Phantom.main(Phantom.java:15) 由于:java.lang.ClassNotFoundException: org.openqa.selenium.browserlaunchers.Proxies - user2044296
你是否使用了代理?请根据出现错误的代码更新问题。如果您正在使用Selenium v 2.44.0,则可能需要升级selenium或phantom.js,因为似乎存在兼容性问题。[链接](https://code.google.com/p/selenium/issues/detail?id=8088) - Manu
1
再次感谢。我之前使用的是Selenium 2.44,现在已经升级到2.46了,虽然它可以工作,但当我使用它时,程序的执行并没有停止,我的意思是说在Eclipse控制台上,“停止”按钮仍然处于活动状态。我认为PhantomJS驱动程序线程仍然处于活动状态,所以除了System.exit(0)之外,是否有其他方法来停止执行? - user2044296
你应该使用 driver.quit(); 关闭驱动程序,否则它将一直存在于内存中。 - Manu
这已经不再是一个解决方案了。类PhantomJSDriver接受其DesiredCapabilities对象,如果在构造函数中未提供,则调用适当的构造函数。public PhantomJSDriver() { this(DesiredCapabilities.phantomjs()); } - danny

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接