Selenium Webdriver 如何處理 Chrome 中的 SSL 證書?


我們可以使用 Chrome 瀏覽器中的 Selenium webdriver 來處理 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 {
      //object of ChromeOptions
      ChromeOptions c = new ChromeOptions();

      //set browser properties
      c.setAcceptInsecureCerts(true);
      System.setProperty("webdriver.chrome.driver",
         "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      // pass browser option to webdriver
      WebDriver driver = new ChromeDriver(c);
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      driver.get("application url to be entered");
      driver.quit();
   }
}

更新日期: 08-Apr-2021

2 千多次瀏覽

開啟你的職業

透過完成教程獲得認證

開始
廣告
© . All rights reserved.