【问题标题】:Chromedriver in Selenium and SSL certificateSelenium 和 SSL 证书中的 Chromedriver
【发布时间】:2011-07-21 09:51:21
【问题描述】:

我正在使用 Selenium 测试一个具有 HTTP Auth 甚至 SSL 证书的网站。

作为 HTTP 基本身份验证的解决方法,我使用 ChromeDriver - http://code.google.com/p/selenium/wiki/ChromeDriver 并以格式打开 URL

https://username:password@my-test-site.com

但现在出于安全原因,需要在 PC 上安装客户端证书才能登录该应用程序。

但是,ChromeDriver 看不到“选择证书”提示,我什至无法将其切换为警报。

有人解决了这个问题吗?

【问题讨论】:

标签: google-chrome selenium ssl-certificate


【解决方案1】:

您可以通过添加具有以下内容的注册表 KEY 来告诉 Chrome 浏览器为特定 URL 使用特定客户端证书:

HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome\AutoSelectCertificateForUrls\1 = "{\"pattern\":\"https://www.example.com\",\"filter\":{\"ISSUER\":{\"CN\":\"cn of issuer\"}}}"

您可以通过在同一分支下添加其他键来添加其他条目。

在 Linux 上稍微复杂一些,因为您需要在以下位置修改 json 格式的首选项:

~/.config/chromium/Default/Preferences

看起来上述选项仅适用于加入 Active Directory 域的计算机。如果上述步骤不起作用您可以尝试使用预配置的模板来引入可从以下 url 下载的更改:https://www.chromium.org/administrators/policy-templates

【讨论】:

    【解决方案2】:

    您可以使用--ignore-certificate-errors 命令行开关告诉 Chrome 忽略不受信任的证书错误,而不是安装客户端证书。

    为此,请创建ChromeDriver 的实例,如下所示:

    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability("chrome.switches", Arrays.asList("--ignore-certificate-errors"));
    driver = new ChromeDriver(capabilities);
    

    【讨论】:

    • 问题是,我尝试访问的站点实际上需要安装客户端证书。而且因为我们有更多的测试环境,每个环境都需要特定的证书……
    • 如何使用独立的 selenium 服务器从命令行执行此操作?
    • @PavelJanicek 你能解决这个问题吗?如何提及在 chrome 驱动程序上选择哪个证书?
    • @Manvi 我无法解决这个问题。最后,我们(测试人员)协商了一个不需要客户端证书的测试环境
    • @PavelJanicek 我能够使用幻像驱动程序解决该问题。我发布了包含我使用的详细信息的解决方案。
    【解决方案3】:

    我用下面的代码解决了这个问题

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

    但我必须使用以下命令将我的 pfx 证书转换为 pem

    openssl pkcs12 -in client_ssl_cert.pfx -out client_ssl_cert.pem -clcerts
    

    【讨论】:

    • 您分享的上述示例,它仍然有效吗?当我尝试以相同的方式实现时,但似乎协商的功能压倒了所需的功能。我也提出了同样的问题。 stackoverflow.com/questions/71530302/…
    【解决方案4】:

    以 Manvi 的回答为基础,我无法使用 PEM 证书或 PFX 证书使其工作,因此我不得不使用 openssl 从我的 PFX 文件中手动提取证书和密钥

    提取证书

    openssl pkcs12 -clcerts -nokeys -in "SourceFile.PFX" -out certificate.crt -password pass:"MyPassword" -passin pass:"MyPassword"
    

    提取密钥

    openssl pkcs12 -nocerts -in "SourceFile.PFX" -out private.key -password pass:"MyPassword" -passin pass:"MyPassword" -passout pass:TemporaryPassword
    

    代码

    File path=new File(phantomJSBinaryFile);
    System.setProperty("phantomjs.binary.path",path.getAbsolutePath());
    
    ChromeOptions cap =new ChromeOptions();
    ImmutableMap<String, String> commandLineArguments = ImmutableMap.<String, 
    String>builder()
        .put("web-security", "false")
        .put("ssl-protocol", "any")
        .put("ignore-ssl-errors", "true")
        .put("webdriver-loglevel", "INFO")
        .put("ssl-client-certificate-file", certificatePath) //certificate.cer
        .put("ssl-client-key-file", keyPath) //private.key
        .put("ssl-client-key-passphrase", pwd)
        .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);
    System.out.println(driver.getTitle());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2012-01-10
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      相关资源
      最近更新 更多