我需要将我的Java程序从ChromeDriver重写为PhantomJS。我想我应该只启用PhantomJS而不是ChromeDriver,仅此而已(我是对的吗?)。我尝试了几种方法来做到这一点,但我总是得到NoClassDefFoundError。
启用ChromeDriver的方式:
System.setProperty("webdriver.chrome.driver", CHROMEDRIVER_PATH);
WebDriver driver = new ChromeDriver();
这就是我试图启用PhantomJS的方式:
DesiredCapabilities DesireCaps = new DesiredCapabilities();
DesireCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, PHANTOMJSDRIVER_PATH);
WebDriver driver = new PhantomJSDriver(DesireCaps);
第二次尝试
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, PHANTOMJSDRIVER_PATH);
WebDriver driver = new PhantomJSDriver();
第三
File src = new File(PHANTOMJSDRIVER_PATH);
System.setProperty("phantomjs.binary.path", src.getAbsolutePath());
WebDriver driver = new PhantomJSDriver();
答案 0 :(得分:2)
我在我的项目中使用如下,它的工作原理。
DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
capabilities.setCapability("phantomjs.binary.path","path/to/phantomjsdriver");
driver = new PhantomJSDriver(capabilities);
另外,请确保您的项目中存在phantomjs依赖项。
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>1.3.0</version>
</dependency>