Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get selenium-webdriver to ignore SSL errors in Firefox and PhantomJS?

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.

like image 477
6axter82 Avatar asked Nov 30 '22 16:11

6axter82


2 Answers

This is only for Javascript / Node.js Selenium Webdriverjs case, only MacOS solution.

  1. 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 in a.

    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);
    
  2. 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.

like image 191
6axter82 Avatar answered Jan 04 '23 22:01

6axter82


For Firefox, you can use the following code:

var driver = new webdriver.Builder().withCapabilities(Capabilities.firefox()
.set("acceptInsecureCerts", true)).build();
like image 32
Chris Avatar answered Jan 04 '23 22:01

Chris