如何使selenium-webdriver忽略Firefox和PhantomJS中的SSL错误? [英] How do I get selenium-webdriver to ignore SSL errors in Firefox and PhantomJS?

查看:494
本文介绍了如何使selenium-webdriver忽略Firefox和PhantomJS中的SSL错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于这些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.

解决方案

这仅适用于Javascript/Node.js Selenium Webdriverjs情况,仅适用于MacOS解决方案.

  1. Firefox案例:

    a.使用用于Firefox的配置文件管理器设置新的配置文件.

    • 使用/Applications/Firefox.app/Contents/MacOS/firefox-bin -P(-profilemanager,也可以使用)打开配置文件管理器
    • 点击创建个人资料"
    • 在第二个创建步骤中,将显示配置文件的路径.复制它!

    b.添加ffprofile文件

    • 转到功能/支持
    • 添加新文件"ffprofile.json"
    • 向文件添加{"ffprofile":"<profilePath>"},其中<profilePath>是您在a.
    • 中复制的路径

    c.将您的本地系统添加到个人资料

    • 使用/Applications/Firefox.app/Contents/MacOS/firefox-bin -P
    • 再次打开Profile Manager
    • 选择您的新配置文件,单击启动Firefox"
    • 在浏览器中,转到您的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);
    

  2. 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']为我做了工作.

希望对您有用.

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.

解决方案

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.

这篇关于如何使selenium-webdriver忽略Firefox和PhantomJS中的SSL错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆