Selenium 简明教程

Selenium - SSL Certificate Error

使用 Selenium Webdriver 可以处理 SSL 证书错误。SSL 是用于在浏览器和服务器之间创建连接的标准化协议。通过 SSL 证书交换的信息已加密,并且它可以验证信息是否发送到正确的服务器。它对网站进行身份验证并提供免遭黑客入侵的保护。如果 SSL 证书有问题,将会抛出一个不受信任的 SSL 证书错误。我们在启动应用程序时应当收到此类错误。

SSL Certificate error can be handled using the Selenium Webdriver. A SSL is the standardized protocol used to create a connection between the browser and server. The information exchanged via a SSL certificate is encrypted and it verifies if the information is sent to the correct server. It authenticates a website and provides protection from hacking. An untrusted SSL certificate error is thrown if there are problems in the SSL certificate. We should receive such an error while launching an application.

Selenium Webdriver 具有 DesiredCapabilities 类来限定浏览器配置文件。Combined with Option 类的 DesiredCapabilities 类可以用于在 Selenium Webdriver 中处理不同浏览器中的 SSL 证书错误。下图显示了一个错误示例,该错误是通过在 Safari 浏览器中打开以下 URL 获得的: https://expired.badssl.com/

Selenium Webdriver has the DesiredCapabilities class to defile profiles for browsers. DesiredCapabilities class used with Option classes can be used to handle SSL certificates error in different browsers in Selenium Webdriver.The below image shows an example of an error, which was obtained by opening the URL: https://expired.badssl.com/ in Safari browser.

selenium ssl certificate error 1

Chrome Browser

要处理 Chrome 中的 SSL 证书,我们必须将 ChromeOptions 类与 DesiredCapabilities 类配合使用。Chrome 中显示的 SSL 错误是 Your connection is not private

To handle SSL certificates in Chrome, we have to use the ChromeOptions class along with the DesiredCapabilities class. The SSL error shown in Chrome is Your connection is not private.

Code Implementation

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.concurrent.TimeUnit;

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

      // Desired Capabilities class to profile Chrome
      DesiredCapabilities dc = new DesiredCapabilities();
      dc.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);

      // Chrome Options class
      ChromeOptions opt = new ChromeOptions();
      opt.merge(dc);

      // Initiate the Webdriver with options
      WebDriver driver = new ChromeDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // launch application with SSL error
      driver.get("https://expired.badssl.com");

      // get browser title
      System.out.println("Browser title in Chrome: " + driver.getTitle());

      // quiting the browser
      driver.quit();
   }
}
Browser title in Chrome: Privacy error

在上述示例中,我们在 Chrome 中处理了 SSL 证书错误并启动了应用程序,然后使用控制台中的消息获取了浏览器标题 - Browser title in Chrome: Privacy error

In the above example, we handled the SSL certificate error in Chrome and launched the application and then obtained the browser title with the message in the console - Browser title in Chrome: Privacy error.

Firefox Browser

要处理 Firefox 中的 SSL 证书,我们必须将 FirefoxOptions 类与 DesiredCapabilities 类配合使用。Firefox 中显示的 SSL 错误是 Warning: Potential Security Risk Ahead

To handle SSL certificates in Firefox, we have to use the FirefoxOptions class along with the DesiredCapabilities class. SSL error shown in Firefox is Warning: Potential Security Risk Ahead.

Code Implementation

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.concurrent.TimeUnit;

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

      // Desired Capabilities class to profile Firefox
      DesiredCapabilities dc = new DesiredCapabilities();
      dc.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);

      // Firefox Options class
      FirefoxOptions opt = new FirefoxOptions();
      opt.merge(dc);

      // Initiate the Webdriver with options
      WebDriver driver = new FirefoxDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // launch application with SSL error
      driver.get("https://expired.badssl.com");

      // get browser title
      System.out.println("Browser title in Firefox: " + driver.getTitle());

      // quitting the browser
      driver.quit();
   }
}
Browser title in Firefox: Privacy error

在上述示例中,我们在 Firefox 中处理了 SSL 证书错误并启动了应用程序,然后使用控制台中的消息获取了浏览器标题 - Browser title in Firefox: Privacy error

In the above example, we had handled the SSL certificate error in Firefox and launched the application and then obtained the browser title with the message in the console - Browser title in Firefox: Privacy error.

Edge Browser

要处理 Edge 中的 SSL 证书,我们必须将 EdgeOptions 类与 DesiredCapabilities 类配合使用。Edge 中显示的 SSL 错误是 Your connection isn’t private

To handle SSL certificates in Edge, we have to use the EdgeOptions class along with the DesiredCapabilities class. SSL error shown in Edge is Your connection isn’t private.

Code Implementation

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.EdgeDriver;
import org.openqa.selenium.firefox.EdgeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.concurrent.TimeUnit;

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

      // Desired Capabilities class to profile Edge
      DesiredCapabilities dc = new DesiredCapabilities();

      // Desired Capabilities class to profile Edge
      DesiredCapabilities dc = new DesiredCapabilities();
      dc.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);

      // Edge Options class
      EdgeOptionsOptions opt = new EdgeOptions();
      opt.merge(dc);

      // Initiate the Webdriver with options
      WebDriver driver = new EdgeDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // launch application with SSL error
      driver.get("https://expired.badssl.com");

      // get browser title
      System.out.println("Browser title in Edge: " + driver.getTitle());

      // quitting the browser
      driver.quit();
   }
}
Browser title in Edge: Privacy error

在上述示例中,我们在 Edge 中处理了 SSL 证书错误并启动了应用程序,然后使用控制台中的消息获取了浏览器标题 - Browser title in Edge: Privacy error

In the above example, we handled the SSL certificate error in Edge and launched the application and then obtained the browser title with the message in the console - Browser title in Edge: Privacy error.

Safari Browser

要处理 Safari 中的 SSL 证书,我们必须将 SafariOptions 类与 DesiredCapabilities 类配合使用。Safari 中显示的 SSL 错误是 This connection is not private

To handle SSL certificates in Safari, we have to use the SafariOptions class along with the DesiredCapabilities class. SSL error shown in Safari is This connection is not private.

Code Implementation

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.SafariDriver;
import org.openqa.selenium.firefox.SafariOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.concurrent.TimeUnit;

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

      // Desired Capabilities class to profile Safari
      DesiredCapabilities dc = new DesiredCapabilities();
      dc.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);

      // Safari Options class
      SafariOptions opt = new SafariOptions();
      opt.merge(dc);

      // Initiate the Webdriver with options
      WebDriver driver = new SafariDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // launch application with SSL error
      driver.get("https://expired.badssl.com");

      // get browser title
      System.out.println("Browser title in Safari: " + driver.getTitle());

      // quitting the browser
      driver.quit();
   }
}
Browser title in Safari: Privacy error

在上述示例中,我们在 Safari 中处理了 SSL 证书错误并启动了应用程序,然后使用控制台中的消息获取了浏览器标题 - Browser title in Safari: Privacy error

In the above example, we had handled the SSL certificate error in Safari and launched the application and then obtained the browser title with the message in the console - Browser title in Safari: Privacy error.

这总结了我们对 Selenium 教程 - SSL 证书错误的全面讲解。我们从描述 SSL 证书错误入手,并逐步讲解了如何使用 Selenium Webdriver 处理不同浏览器中的 SSL 证书错误。这为你提供了关于 Selenium - SSL 证书错误的深入知识。明智的做法是不断练习你学到的内容并探索其他与 Selenium 相关的知识来加深你的理解并拓展你的视野。

This concludes our comprehensive take on the tutorial on Selenium - SSL Certificate Error. We’ve started with describing what SSL Certificate errors, and walked through examples of how to handle SSL Certificate errors in different browsers with Selenium Webdriver. This equips you with in-depth knowledge of the Selenium - SSL Certificate Error. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.