How to deal with security certificates using Selenium?

SeleniumAutomation TestingTesting Tools

We can deal with security certificates using Selenium webdriver. We can have certificates like the SSL certificate and insecure certificate. All these can be handled with the help of the DesiredCapabilities and ChromeOptions class.

We shall create an object of the DesiredCapabilities class and apply setCapability method on it. Then pass the CapabilityType and value as parameters to that method.

These general browser chrome profiles shall be fed to the object of the ChromeOptions class for the local browser with the help of the merge method. Finally, this information needs to be passed to the webdriver object.

Syntax

DesiredCapabilities c=DesiredCapabilities.chrome();
c.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
c.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
ChromeOptions opt= new ChromeOptions();
opt.merge(c);

Example of a SSL certificate is shown below. We need to click on the Proceed anyway button to accept this certificate.

Example of Insecure certificate is shown below −

Example

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class SecurityCertAccepts{
   public static void main(String[] args) {
      //DesiredCapabilities object
      DesiredCapabilities c=DesiredCapabilities.chrome();
      //accept insecure certificates
      c.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
      //accept SSL certificates
      c.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
      //ChromeOptions object
      ChromeOptions opt= new ChromeOptions();
      //merging browser profiles
      opt.merge(c);
      System.setProperty("webdriver.chrome.driver",
      "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      // configure options parameter to Chrome driver
      WebDriver driver = new ChromeDriver(opt);
   }
}
raja
Published on 02-Feb-2021 11:49:02
Advertisements