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

7
我正在Windows 7上使用Selenium Web Driver。 我试图测试一个需要身份验证并且我需要使用SSL证书的网站。 当我在Selenium之外使用Firefox时一切正常,但是我注意到Selenium打开的Firefox浏览器会话没有注册任何证书,所以很明显不起作用。以下是我使用Firefox“out” of Selenium时的高级首选项:

enter image description here

下面是我使用Selenium打开的Firefox会话的高级首选项:

enter image description here

我已经尝试保持会话打开并手动注册证书,但浏览器会话没有注册证书。以下是我的代码,如果可能会有用:
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();

        }
}

有任何建议吗?
3个回答

7

我解决了!

在网上冲浪时,我找到了这篇文章 http://seleniummonk.blogspot.it/p/how-to-handle-ssl-cerificates.html ,为我提供了解决方案。

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

以下是新的代码,它可以正常工作。

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

        }

}

我希望这能有用!


1
你可以使用代理来实现。通过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);

SeleniumHQ获取的代码


我已经尝试将你的代码行放入我的代码中,但不幸的是没有任何变化...(我已经在上面更新了我的原始请求,并附上了一些图片,希望它们能有所帮助...) - Cesare

0

首先在CMD中使用以下命令创建一个像我在Firefox中创建的名为“Test”的配置文件:

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

然后使用生成证书时使用的密码,将您的证书导入到这个新创建的Firefox配置文件中。 在我的情况下,这是一个P12扩展证书。

完成后,您可以在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);

背景 我正在使用Firefox 56.0和Windows 7


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接