2017-02-16 141 views
1

我需要將我的Java程序從ChromeDriver重寫到PhantomJS。我想我應該啓用PhantomJS而不是ChromeDriver,而不是更多(我是對的?)。我嘗試了幾種方法來做到這一點,但我總是得到NoClassDefFoundError。如何在Selenium中啓用PhantomJS WebDriver?

我能夠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(); 

回答

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> 
+0

謝謝。我在我的pom中擁有detro依賴項1.2.0。現在它工作了! –

相關問題