How to use Phantomjs to open a website in Selenium
I am trying to use PhantomJs headless web kit to open google.com via selenium web driver, but the system throws an error when executing the code. phantomJs.exe is located in the E directory. Please help me solve this problem.
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");
}
Mistake:
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable file must be set to phantomjs.binary.path Property / system property / PATH variable; See https://github.com/ariya/phantomjs/wiki for more information . 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.phantjomjsri.ri.ri. phantomjs.PhantomJSDriver. (PhantomJSDriver.java:94) at multidrivers.main (multidrivers.java:35)
source to share
Exception on thread "main" java.lang.IllegalStateException: The path to the driver executable must be set using the phantomjs.binary.path / system property / PATH variable;
The above issue is due to the driver not being initialized with the DesiredCapabilities object:
WebDriver driver = new PhantomJSDriver();
Updating the code as shown below should fix your problem:
WebDriver driver = new PhantomJSDriver(caps);
Let me know if you have any questions.
source to share