• 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

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.

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

Output

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

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

Output

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

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

Output

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

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

Output

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.

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.

Advertisements