如何使用Phantomjs在Selenium中开设网站

时间:2015-07-27 14:16:58

标签: selenium phantomjs

我试图使用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");

            }

错误:

  

线程中的异常" main" 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)       at multidrivers.main(multidrivers.java:35)

1 个答案:

答案 0 :(得分:5)

  

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

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

WebDriver driver = new PhantomJSDriver();      

如下更新代码可以解决您的问题:

WebDriver driver = new PhantomJSDriver(caps);  

如果您有任何疑问,请与我们联系。