Selenium Webdriver 如何在 Chrome 中处理 SSL 证书?

seleniumautomation testingtesting tools

我们可以在 Chrome 浏览器中使用 Selenium Webdriver 处理 SSL 证书。SSL 是用于在浏览器和服务器之间建立连接的标准化协议。

通过 SSL 证书交换的信息经过加密,SSL 证书会验证信息是否发送到正确的服务器。它可以验证网站身份并提供防黑客保护。

如果 SSL 证书存在问题,则会抛出"不受信任的 SSL 证书"错误。我们在启动网站时会收到此类错误。在 Chrome 中,我们使用 ChromeOptions 类来处理 SSL 证书。

我们将创建此类的实例,并将 setAcceptInsecureCerts 功能设置为 true。最后,将 Chrome 浏览器的此属性传递给 webdriver 对象。

语法

ChromeOptions c = new ChromeOptions();
c.setAcceptInsecureCerts(true);

示例

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.WebDriver;
public class SSLErrorChrome {
   public static void main(String[] args) throws IOException {
      //ChromeOptions 对象
      ChromeOptions c = new ChromeOptions();

      //设置浏览器属性
      c.setAcceptInsecureCerts(true);
      System.setProperty(&"webdriver.chrome.driver",
&"C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      // 将浏览器选项传递给 webdriver
      WebDriver driver = new ChromeDriver(c);
      //隐式等待
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL 启动
      driver.get("application url to be entered");
      driver.quit();
   }
}

相关文章