如何将自己的证书(.pem文件)添加到硒chromedriver?

时间:2019-01-09 14:21:06

标签: java selenium ssl-certificate selenium-chromedriver pem

我有一个docker-compose文件:

version: '3'
   services:
      selenium-3-chrome:
      image: selenium/standalone-chrome-debug:3.14.0
      restart: always
environment:
  TZ: Europe/Budapest
  SCREEN_WIDTH: 1920
  SCREEN_HEIGHT: 1080
  JAVA_OPTS: -Xmx128m
ports:
  - 4444:4444
  - 5900:5900
volumes:
  - /dev/shm:/dev/shm

我的Java代码:

        ChromeOptions chromeOptions = util.chromeOptions(scenario);
        chromeOptions.setHeadless(true);//default-bol is az
        chromeOptions.addArguments("--window-size=1366,768");

        DesiredCapabilities capability = DesiredCapabilities.chrome();
        capability.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

        initProperies();
        if(config == null) {
            readConfiguration();
            try {
                host = System.getProperty(HOSTNAME);
            } catch(Exception e) {
                 //...
            }
        }
        // itt állítom rá a docker-es chromedriverre:
        WebDriver driver = new RemoteWebDriver(new URL(config.getConnection().getWebDriverUrl()), capability);

        driver.manage().timeouts().pageLoadTimeout(90000, TimeUnit.MILLISECONDS);

        driver.get(host+"/");

如何将自己的证书添加到功能或chromeOptions?在Chrome浏览器中为“ /home/../rootca/lbsca.pem”->,它在“ Authorities”证书中,而不在“您的证书”中。或者有可能我可以将.pem添加到docker的独立浏览器中? 我希望我能理解。对不起我的英语

1 个答案:

答案 0 :(得分:0)

PhantomJSDriver可用于执行此功能。

DesiredCapabilities cap = DesiredCapabilities.chrome();
ImmutableMap<String, String> commandLineArguments = ImmutableMap.<String, 
String>builder()
    .put("web-security", "false")
    .put("ssl-protocol", "any")
    .put("ignore-ssl-errors", "true")
    .put("webdriver-loglevel", "DEBUG")
    .put("ssl-client-certificate-file", certificatePath)
    .put("ssl-client-key-passphrase", certificatePassword)
    .build();
String[] params = commandLineArguments.entrySet().stream()
    .map(e -> String.format("--%s=%s", e.getKey(), e.getValue()))
    .collect(Collectors.toList())
    .toArray(new String[0]);
cap.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, params);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new PhantomJSDriver(cap);
driver.get(Url);

如果不是必需的证书,则意味着您可以通过接受不安全的证书来跳过证书控制,可以通过以下功能来跳过证书控制:

Chrome和Internet Explorer

capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capability.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true)

Firefox

首先,您需要创建一个配置文件,例如exampleProfile
然后,使用下面的代码在脚本中访问该配置文件;

ProfilesIni profIni = new ProfilesIni();        
FirefoxProfile exampleProfile = profIni.getProfile("exampleProfile");

之后,您需要设置AcceptUntrustedCertificatesAssumeUntrustedCertificateIssuer属性。

exampleProfile.setAcceptUntrustedCertificates(true);
exampleProfile.setAssumeUntrustedCertificateIssuer(false);

最后,您可以使用以下代码初始化驱动程序;

WebDriver driver = new FirefoxDriver(exampleProfile);