[英]Set user-agent with selenium-webdriver and phantomjs in nodejs
[英]Setting service_args for phantomjs in selenium-webdriver for node
我需要能夠使用以下arg運行phantomjs:
- 忽略SSL的錯誤=真
我正在測試的頁面使用自簽名證書,因此我需要arg打開頁面。 我正在嘗試使用以下代碼片段在webdriver中傳遞arg:
capabilities = webdriver.Capabilities.phantomjs();
capabilities.set('service_args', '--ignore-ssl-errors=true');
driver = new webdriver.Builder().
withCapabilities(capabilities).
build();
是傳遞service_args的正確方法嗎? 我實際上希望不要,因為我無法加載測試頁。 我可以通過運行以下內容打開頁面:
phantomjs --ignore-ssl-errors=true myTest.js
這是myTest.js中的代碼
var page = new WebPage();
page.open('https://my.somefaketestpage.com/', function (status) {
just_wait();
});
function just_wait() {
setTimeout(function() {
page.render('screenshot.png');
phantom.exit();
}, 2000);
}
正確答案是:
caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {"--web-security=no", "--ignore-ssl-errors=yes"});
driver = new PhantomJSDriver(caps);
萬一有人需要它用於facebook / php-webdriver,可以通過以下方式將CLI參數傳遞給PhantomJS:
$driver = RemoteWebDriver::create('http://localhost:4444/wd/hub', [
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::PHANTOMJS,
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
'phantomjs.cli.args' => ['--ignore-ssl-errors=true']
]);
讀完這篇文章后,我感到非常困惑,因為可接受的答案是在Java中出現的,而且GhostDriver常量和內容不存在。 對於那些也感到困惑的人,這對我有用:
var webdriver = require('selenium-webdriver'),
Capabilities = webdriver.Capabilities;
var capability = Capabilities
.phantomjs()
.set('phantomjs.cli.args', '--ignore-ssl-errors=true');
var driver = new webdriver
.Builder()
.withCapabilities(capability)
.build();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.