鉴于这些Node依赖项:
{
"chromedriver": "^2.24.1",
"cucumber": "^1.3.0",
"geckodriver": "^1.1.2",
"phantomjs-prebuilt": "^2.1.12",
"selenium-webdriver": "^3.0.0-beta-2"
}
我希望PhantomJS和Firefox忽略SSL证书。以下是我的browser.js的外观:
require('geckodriver');
// main browser object
var browserHandle;
// load selenium webdriver and some rules
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
// load phantomjs into webdriver capabilities
var phantomjs_exe = require('phantomjs-prebuilt').path;
var customPhantom = webdriver.Capabilities.phantomjs();
customPhantom.set("phantomjs.binary.path", phantomjs_exe);
webdriver.Builder()
//.forBrowser('firefox')
//.forBrowser('phantomjs')
.withCapabilities(customPhantom)
.build();
--ignore-ssl-errors=yes
的任何建议?如何在代码中实现它?我想只使用JavaScript而不是Java。
答案 0 :(得分:7)
这仅适用于Javascript / Node.js Selenium Webdriverjs案例,仅适用于MacOS解决方案。
Firefox案例:
一个。使用Profile Manager for Firefox设置新配置文件。
/Applications/Firefox.app/Contents/MacOS/firefox-bin -P
(-profilemanager
打开配置文件管理器,这也适用)湾添加ffprofile文件
<profilePath>
”}添加到文件中,其中<profilePath>
是您在a.
中复制的路径℃。将本地系统添加到配置文件
/Applications/Firefox.app/Contents/MacOS/firefox-bin -P
https://...
。你应该看到一个页面告诉你证书有问题d。将其添加到您的browser.js或以编程方式调用浏览器的地方:
var webdriver = require('selenium-webdriver'),
firefox = require('selenium-webdriver/firefox'),
var ffProfileFile = require('./ffprofile');
var ffProfile = new firefox.Profile(ffProfileFile['ffprofile']);
var ffOptions = new firefox.Options().setProfile(ffProfile);
return browserHandle = new firefox.Driver(ffOptions);
Phantomjs案例(我在这里有两个解决方案,选择一个对你更好的解决方案):
解决方案1:
var webdriver = require('selenium-webdriver'),
phantom = require('phantomjs-prebuilt');
var capabilities = webdriver.Capabilities.phantomjs();
capabilities.set(webdriver.Capability.ACCEPT_SSL_CERTS, true);
capabilities.set(webdriver.Capability.SECURE_SSL, false);
capabilities.set("phantomjs.cli.args",
["--web-security=no",
"--ssl-protocol=any",
"--ignore-ssl-errors=yes"]
);
return browserHandle = new webdriver
.Builder()
.withCapabilities(capabilities)
.build();
解决方案2:
var webdriver = require('selenium-webdriver'),
phantom = require('phantomjs-prebuilt');
var capabilities = {
'browserName' : 'phantomjs',
'phantomjs.cli.args': ['--ignore-ssl-errors=true',
'--ssl-protocol=any', '--web-security=false']
}
return browserHandle = new webdriver
.Builder()
.withCapabilities(capabilities)
.build();
对于这一个'phantomjs.cli.args': ['--ignore-ssl-errors=true']
为我做了这份工作。
希望它对你有用。
答案 1 :(得分:2)
对于Firefox,您可以使用以下代码:
var driver = new webdriver.Builder().withCapabilities(Capabilities.firefox()
.set("acceptInsecureCerts", true)).build();
答案 2 :(得分:2)
对于Chrome浏览器,只需设置Capability.ACCEPT_INSECURE_TLS_CERTS, true
。它对我有用。
const { Builder, By, until, Capabilities, Capability } = require('selenium-webdriver');
const capabilities = Capabilities.chrome();
capabilities.set(Capability.ACCEPT_INSECURE_TLS_CERTS, true);
this.driver = new Builder()
.withCapabilities(capabilities)
.forBrowser('chrome')
.build();