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

How can I add my own certificate (.pem file) to my selenium chromedriver?

我有一个 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+"/");

如何将我自己的证书添加到功能或chrome选项中? "/home/../rootca/lbsca.pem" -> 在 chrome 浏览器中,它在 "Authorities" 证书中,而不是在 "your certificates" 中。或者我可以将我的 .pem 添加到 docker 的独立-chrome? 我希望,我理解..对不起我的英语

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");

之后,您需要设置 AcceptUntrustedCertificates & AssumeUntrustedCertificateIssuer 属性。

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

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

WebDriver driver = new FirefoxDriver(exampleProfile);