How to enable PhantomJS WebDriver in Selenium?

≯℡__Kan透↙ 提交于 2019-12-11 05:52:00

问题


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

回答1:


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>


来源:https://stackoverflow.com/questions/42273403/how-to-enable-phantomjs-webdriver-in-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!