How to use Phantomjs to open a website in Selenium

I am trying to use PhantomJs headless web kit to open 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.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "E:\\phantomjs.exe");
                WebDriver driver = new PhantomJSDriver();              




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 for more information . The latest version can be downloaded from at ( at org.openqa.selenium.phantomjs.PhantomJSDriverService.findPhantomJS ( at org.openqa.selenium.phantomjs.PhantomJSDriverService.createDefaultService ( at org.openqa.selenium.phantjomjsri.ri.ri. phantomjs.PhantomJSDriver. ( at multidrivers.main (


source to share

1 answer

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.



All Articles