• Selenium Video Tutorials

Selenium - SSL Certificate Error



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 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

Handing SSL Certificate Error in Chrome Browser

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.

Example

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();
   }
}

Output

Browser title in Chrome: Privacy error

Process finished with exit code 0

In the above example, we had 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.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Handing SSL Certificate Error in Firefox Browser

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.

Example

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();
   }
}

Output

Browser title in Firefox: Privacy error

Process finished with exit code 0

In the above example, we had 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.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Handing SSL Certificate Error in Edge Browser

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.

Example

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();
   }
}

Output

Browser title in Edge: Privacy error

Process finished with exit code 0

In the above example, we had 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.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Handing SSL Certificate Error in Safari Browser

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.

Example

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();
   }
}

Output

Browser title in Safari: Privacy error

Process finished with exit code 0

In the above example, we had 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.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Advertisements