我正在尝试使用 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/