【问题标题】:How to use SSL certificates in Selenium Web Driver?如何在 Selenium Web Driver 中使用 SSL 证书?
【发布时间】:2015-06-16 13:17:17
【问题描述】:

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

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

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

当我使用 Firefox “out” Selenium 时,这里是高级首选项

在这里你和我使用 Selenium 打开的 Firefox 会话时是一样的

我已尝试保持会话打开并手​​动注册证书,但浏览器会话未注册证书。

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

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

        }
}

有什么建议吗?

【问题讨论】:

    标签: java selenium ssl selenium-webdriver


    【解决方案1】:

    我已经解决了!

    在网上冲浪我发现这篇帖子 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();
    
            }
    
    }
    

    我希望这可能有用!

    【讨论】:

      【解决方案2】:

      您可以使用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);
      

      代码取自SeleniumHQ

      【讨论】:

      • 我尝试将您的代码行放入我的代码中,但不幸的是没有任何改变......(我已经用几张我希望有用的图片更新了我上面的原始请求...... )
      【解决方案3】:

      首先在 Firefox 中使用 CMD 中的“测试”创建一个配置文件:

      "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

      【讨论】:

        猜你喜欢
        • 2016-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-30
        • 1970-01-01
        • 1970-01-01
        • 2019-01-05
        • 1970-01-01
        相关资源
        最近更新 更多