Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Phantomjs for opening a website in Selenium

I'm trying to use headless webkit of PhantomJs for opening google.com through selenium webdriver but when I execute the code system throws an error. phantomJs.exe is placed in E directory. Please help me resolve this issue.

     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");

            }

Error:

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)

like image 310
user2044296 Avatar asked Oct 15 '25 19:10

user2044296


1 Answers

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;

The above issue is due to the driver not being initialized with the DesiredCapabilities object:

WebDriver driver = new PhantomJSDriver();      

Updating the code as below should solve your issue:

WebDriver driver = new PhantomJSDriver(caps);  

Let me know if you have any queries.

like image 56
Manu Avatar answered Oct 19 '25 14:10

Manu