如何使用Selenium 3.4.0独立版处理Firefox,Chrome和IE浏览器的SSL证书

时间:2018-02-21 11:33:31

标签: selenium selenium-webdriver webdriver

我的应用程序同时具有http和https ssl证书。要使用Selenium 3.4.0处理https链接,我使用了以下firefox代码。 Firefox浏览器正在打开,但它显示“安全连接失败”

System.setProperty("webdriver.gecko.driver","C:\\Users\\vidhya.r\\Desktop\\Automation\\Jars\\geckodriver.exe");
FirefoxProfile pro = new FirefoxProfile();
pro.setAcceptUntrustedCertificates(true);
WebDriver driver = new FirefoxDriver(pro);
driver.get("https://192.168.8.115:7077/final/#!");

2 个答案:

答案 0 :(得分:0)

我正在使用帮助类TrustConnection:

import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;

public class TrustConnection extends BaseOfTests {
    public static void trustConnection() throws Exception {
        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };

        // Install the all-trusting trust manager
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };

        // Install the all-trusting host verifier
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

        URL url = new URL(baseUrl);
        URLConnection con = url.openConnection();
        Reader reader = new InputStreamReader(con.getInputStream());
        while (true) {
            int ch = reader.read();
            if (ch == -1) {
                break;
            }
        }
    }
}

其中BaseOfTests是我的主框架类baseUrl变量。然后在我启动浏览器的方法中,我只是添加: TrustConnection.trustConnection();

答案 1 :(得分:0)

以下解决方案绝对适用于您的Chrome浏览器。

    WebDriverManager.chromedriver().setup();
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--ignore-certificate-errors");
    System.setProperty("webdriver.chrome.driver", 
    System.getProperty("user.dir")+"/Drivers/chromedriver.exe");
    driver = new ChromeDriver(options);
    driver.get(applicationURL);
    driver.manage().window().maximize();