Stas Mackarow
Stas Mackarow

Reputation: 195

How to enable PhantomJS WebDriver in Selenium?

I need to rewrite my Java program from ChromeDriver to PhantomJS. I think i should just enable PhantomJS instead of ChromeDriver and nothing more (am i right?). I tried a few ways to do this, but i always get NoClassDefFoundError.

My way of enabling ChromeDriver:

System.setProperty("webdriver.chrome.driver", CHROMEDRIVER_PATH);
WebDriver driver = new ChromeDriver();

And this is how i tried to enable PhantomJS:

 DesiredCapabilities DesireCaps = new DesiredCapabilities();
 DesireCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, PHANTOMJSDRIVER_PATH);
 WebDriver driver = new PhantomJSDriver(DesireCaps);

Second try

DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, PHANTOMJSDRIVER_PATH);
WebDriver driver = new PhantomJSDriver();

Third

File src = new File(PHANTOMJSDRIVER_PATH);
System.setProperty("phantomjs.binary.path", src.getAbsolutePath());
WebDriver driver = new PhantomJSDriver();

Upvotes: 2

Views: 4496

Answers (1)

Gaurang Shah
Gaurang Shah

Reputation: 12910

I have used as below in my project and it works.

DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
capabilities.setCapability("phantomjs.binary.path","path/to/phantomjsdriver");
driver = new PhantomJSDriver(capabilities);

Also, make sure you have phantomjs dependency in your project.

<dependency>
    <groupId>com.codeborne</groupId>
    <artifactId>phantomjsdriver</artifactId>
    <version>1.3.0</version>
</dependency>

Upvotes: 3

Related Questions