selenium - 如何使用 Phantomjs 在 Selenium 中打开网站

标签 selenium phantomjs

我正在尝试使用 PhantomJs 的 headless 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");

            }

错误:

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the phantomjs.binary.path capability/system property/PATH variable; for more information, see https://github.com/ariya/phantomjs/wiki. The latest version can be downloaded from http://phantomjs.org/download.html at com.google.common.base.Preconditions.checkState(Preconditions.java:197) at org.openqa.selenium.phantomjs.PhantomJSDriverService.findPhantomJS(PhantomJSDriverService.java:236) at org.openqa.selenium.phantomjs.PhantomJSDriverService.createDefaultService(PhantomJSDriverService.java:181) at org.openqa.selenium.phantomjs.PhantomJSDriver.(PhantomJSDriver.java:104) at org.openqa.selenium.phantomjs.PhantomJSDriver.(PhantomJSDriver.java:94) at multidrivers.main(multidrivers.java:35)

最佳答案

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the phantomjs.binary.path capability/system property/PATH variable;

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

WebDriver driver = new PhantomJSDriver();      

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

WebDriver driver = new PhantomJSDriver(caps);  

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

关于selenium - 如何使用 Phantomjs 在 Selenium 中打开网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31655613/

相关文章:

ffmpeg - 使用 ffmpeg image2pipe 和 phantomjs 从网页屏幕截图渲染视频时出错

javascript - 在控制台中发出 http 请求时,PhantomJs 不返回任何状态

angularjs - 我无法让 phantomjs 单击登录按钮

javascript - Phantomjs打不开网页

java - 如何在 POJO 类中存储 TestNG 数据提供程序值

java - Selenium Java - 通过 JavascriptExecutor 获取页面源

ruby - Capybara + Cucumber + Selenium Driver 的 Authlogic 不工作

javascript - 使用 casperjs 处理 jquery 事件

python - Instagram/Selenium 未点击接受按钮

php - Selenium 测试在执行 javascript 时随机崩溃