问题
Given these Node dependencies:
{
"chromedriver": "^2.24.1",
"cucumber": "^1.3.0",
"geckodriver": "^1.1.2",
"phantomjs-prebuilt": "^2.1.12",
"selenium-webdriver": "^3.0.0-beta-2"
}
I would like PhantomJS and Firefox to ignore SSL certificates. Here is how my browser.js looks:
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();
Any suggestions with --ignore-ssl-errors=yes
? How can I implement it in the code? I want to use only JavaScript, rather than Java.
回答1:
This is only for Javascript / Node.js Selenium Webdriverjs case, only MacOS solution.
Firefox case:
a. Setup a new Profile using the Profile Manager for Firefox.
- Open the Profile Manager with
/Applications/Firefox.app/Contents/MacOS/firefox-bin -P
(-profilemanager
, this works as well) - Click "Create Profile"
- In the second Step of creation, the Path to the Profile File is displayed. Copy it!
b. Add the ffprofile File
- Go to features/support
- Add a new File "ffprofile.json"
- add { "ffprofile": "
<profilePath>
"} to the file, where<profilePath>
is the Path you copied ina.
c. Add your local system to the Profile
- Open the Profile Manager again with
/Applications/Firefox.app/Contents/MacOS/firefox-bin -P
- Select your new Profile, click "Start Firefox"
- In the Browser, go to your
https://...
. You should see a Page telling you there is a Problem with the certificate - Click on "Advanced" -> "Add Exception" -> "Confirm Security Exception"
d. Add this into your browser.js or where you call a browser programmatically:
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);
- Open the Profile Manager with
Phantomjs case ( I have got two solutions here, pick up the one which is better for you):
solution 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();
solution 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();
for this one the
'phantomjs.cli.args': ['--ignore-ssl-errors=true']
did the job for me.
Hope it will be useful for you.
回答2:
For Firefox, you can use the following code:
var driver = new webdriver.Builder().withCapabilities(Capabilities.firefox()
.set("acceptInsecureCerts", true)).build();
来源:https://stackoverflow.com/questions/39638830/how-do-i-get-selenium-webdriver-to-ignore-ssl-errors-in-firefox-and-phantomjs