java 如何在 Selenium Web Driver 中使用 SSL 证书?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30868708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:46:29  来源:igfitidea点击:

How to use SSL certificates in Selenium Web Driver?

javaseleniumsslselenium-webdriver

提问by Cesare

I'm using Selenium Web Driver on Windows 7.

我在 Windows 7 上使用 Selenium Web 驱动程序。

I'm trying to test a website that use authentication and I need to use SSL certificates.

我正在尝试测试使用身份验证的网站,我需要使用 SSL 证书。

When I use Firefox out of Selenium all works fine but I've noted that the Firefox browser session opened by Selenium doesn't have any certificates registered and so it's clear that it doesn't work.

当我在 Selenium 之外使用 Firefox 时一切正常,但我注意到 Selenium 打开的 Firefox 浏览器会话没有注册任何证书,因此很明显它不起作用。

Here you are the Advanced Preferences when I use Firefox "out" of Selenium

这是我在 Selenium 之外使用 Firefox 时的高级首选项

enter image description here

在此处输入图片说明

and here you are the same when I use the Firefox sessione opened by Selenium

在这里,当我使用 Selenium 打开的 Firefox sessione 时,您是一样的

enter image description here

在此处输入图片说明

I've tried to keep the session opened and to register manually the certificate but the browser session doesn't register the certificate.

我试图保持会话打开并手​​动注册证书,但浏览器会话没有注册证书。

Here you are my code if could be useful

如果有用的话,这是我的代码

package myTestProjects;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class GAMOPERA_Test_01 {

private static WebDriver driver = null;

public static void main(String[] args)  throws InterruptedException {

    // Create a new instance of the Firefox driver
    System.out.println("Creo una nuova sessione del browser Firefox ...");
    driver = new FirefoxDriver();

    //Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    // It is always advisable to Maximize the window before performing DragNDrop action
    System.out.println("Massimizzo la finestra del browser ...");
    driver.manage().window().maximize();
    Thread.sleep(3000L);

    //Launch the Sistema Piemonte Home Page
    System.out.println("Mi collego a Sistema Piemonte ...");
    driver.get("http://<my_site_url>");
    Thread.sleep(3000L);          

    // Find the element Accedi o 
    System.out.println("Accesso tramite certificato digitale ...");
    driver.findElement(By.xpath("/html/body/div[6]/div/div/div[2]/form/table/tbody/tr[3]/td/input")).click();        

    //driver.findElement(By.className("loginbutton")).click();
    Thread.sleep(3000L); 

    // Print TEST = OK!!
    System.out.println("TEST = OK !!");
    //driver.quit();

        }
}

Any suggestions?

有什么建议?

采纳答案by Cesare

I've solved!

我已经解决了!

Surfing on the web I've found this post http://seleniummonk.blogspot.it/p/how-to-handle-ssl-cerificates.htmlthat gave me the solution.

在网上冲浪我发现这篇文章http://seleniummonk.blogspot.it/p/how-to-handle-ssl-cerificates.html给了我解决方案。

I need to use the "Firefox profile" (I use the default one ...), so I can have all the certificates I need to.

我需要使用“Firefox 配置文件”(我使用默认的配置文件...),这样我就可以获得我需要的所有证书。

Here you're the new code that works

这是有效的新代码

package myTestProjects;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class GAMOPERA_Test_01 {

private static WebDriver driver = null;

public static void main(String[] args)  throws InterruptedException {

    ProfilesIni profile = new ProfilesIni();
    FirefoxProfile ffProfile = profile.getProfile("default"); 

    // Create a new instance of the Firefox driver
    System.out.println("Creo una nuova sessione del browser Firefox ...");
    driver = new FirefoxDriver(ffProfile);          

    //Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    // It is always advisable to Maximize the window before performing DragNDrop action
    System.out.println("Massimizzo la finestra del browser ...");
    driver.manage().window().maximize();
    Thread.sleep(3000L);

    //Launch the Sistema Piemonte Home Page
    System.out.println("Mi collego a Sistema Piemonte ...");
    driver.get("<my_site_url>");
    Thread.sleep(3000L);          

    // Find the element Accedi o 
    System.out.println("Accesso tramite certificato digitale ...");
    driver.findElement(By.xpath("/html/body/div[6]/div/div/div[2]/form/table/tbody/tr[3]/td/input")).click();        

    //driver.findElement(By.className("loginbutton")).click();
    Thread.sleep(3000L); 

    // Print TEST = OK!!
    System.out.println("TEST = OK !!");
    //driver.quit();

        }

}

I hope this could be useful!

我希望这可能有用!

回答by Saifur

You can do that using Proxy. Through DesiredCapabilitiesyou can configure the browser accordingly.

您可以使用Proxy. 通过DesiredCapabilities您可以相应地配置浏览器。

String PROXY = "localhost:8080";

org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
     .setFtpProxy(PROXY)
     .setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);

WebDriver driver = new InternetExplorerDriver(cap);

Code taken from SeleniumHQ

代码取自SeleniumHQ

回答by Dhinchak Dhoom

First create a profile as I created with "Test" in Firefox using below command in CMD:

首先使用 CMD 中的以下命令创建我在 Firefox 中使用“测试”创建的配置文件:

"C:\Program Files\Mozilla Firefox\firefox.exe" -P

Then import your certificate in this newly created Firefox Profile with Password used to generate the certificate. In my case this was a P12 extenstion Certificate.

然后在这个新创建的 Firefox 配置文件中导入您的证书,并使用用于生成证书的密码。就我而言,这是 P12 扩展证书。

Once done you can use below code in Selenium and Firefox will not popup for Certificate and you will be logged into the Portal or Website.

完成后,您可以在 Selenium 中使用以下代码,Firefox 不会弹出证书,您将登录到门户或网站。

ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile myProfile = allProfiles.getProfile("Test");
myProfile.setPreference("security.default_personal_cert", "Select Automatically");
FirefoxOptions firefoxoptions = new FirefoxOptions();
firefoxoptions.setProfile(myProfile);
WebDriver driver = new FirefoxDriver(firefoxoptions);

Background I am using Firefox 56.0 and Windows 7

背景 我使用的是 Firefox 56.0 和 Windows 7