我的硒版本是:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
我想通过参数设置驱动程序的端口:--webdriver
,如下所示:
DesiredCapabilities sCaps = new DesiredCapabilities();
sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
"/Users/user/phantomjs");
sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "userAgent",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "javascriptEnabled", true);
sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "loadImages", true);
sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "cookiesEnabled", true);
sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language", "en");
sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Connection", "keep-alive");
ArrayList<String> cliArgsCap = new ArrayList<String>();
cliArgsCap.add("--web-security=false");
cliArgsCap.add("--ssl-protocol=any");
cliArgsCap.add("--ignore-ssl-errors=true");
cliArgsCap.add("--debug=true");
cliArgsCap.add("--webdriver=127.0.0.1:36000");
sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
PhantomJSDriver d = new PhantomJSDriver(sCaps);
但是在我设置了这样的代码后,控制台显示:
Exception in thread "main" org.openqa.selenium.WebDriverException: Timed out
waiting for driver server to start.
selenium不能处理参数--webdriver
吗?
答案 0 :(得分:0)
您必须下载phantomjs的二进制文件,然后将其传递给PhantomJSDriver。 在maven依赖中,你只使用selenium而不是phantomjs
http://phantomjs.org/download.html
现在添加System.setPropertybefore驱动程序实例
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true); // not really needed: JS enabled by default
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:/phantomjs.exe");
WebDriver driver = new PhantomJSDriver(caps);
请参阅以下链接以获取更多信息: -
http://seleniumworks.blogspot.in/2013/03/headless-browser-testing-using.html
使用下面的代码,这是一种为PhantomJSDriver设置端口的方法: -
private void loadLightWeightDriverCustom() {
ArrayList<String> cliArgsCap = new ArrayList();
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, PHANTOMJS_EXE.getAbsolutePath());
cliArgsCap.add("--web-security=false");
cliArgsCap.add("--ssl-protocol=any");
cliArgsCap.add("--ignore-ssl-errors=true");
capabilities.setCapability(
PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
driver = new PhantomJSDriver(Configure(cliArgsCap), capabilities);
}
private PhantomJSDriverService Configure(ArrayList<String> cap) {
return new PhantomJSDriverService.Builder().usingPhantomJSExecutable(PHANTOMJS_EXE)
.usingPort(5555)
.usingCommandLineArguments(
(cap.toArray(new String[cap.size()])))
.build();
}