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

问题描述

给定这些节点依赖性:

{
    "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证书.这是我的浏览器的外观:

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 case,只有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
    • 再次打开配置文件管理器
    • 选择您的新个人资料,单击"开始Firefox"
    • 在浏览器中,转到您的https://....您应该看到一个页面告诉您证书有问题
    • 单击"高级" - >"添加异常" - >"确认安全异常"

    d.将其添加到您的浏览器.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']为我完成了工作.

希望它对您有用.

其他推荐答案

对于Firefox,您可以使用以下代码:

var driver = new webdriver.Builder().withCapabilities(Capabilities.firefox()
.set("acceptInsecureCerts", true)).build();

其他推荐答案

对于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();

本文地址:https://www.itbaoku.cn/post/1740054.html