How to handle the SSL certificate in Selenium Web Driver

The Selenium framework helps in automating browser and web application testing. It is free, open-source, and considered one of the most widely used test automation tools.

SSLSecure Sockets Layer is a networking transparent protocol designed for establishing authenticated and secure connections between web clients and web servers over an insecure network. This protocol guarantees that data is communicated without being changed or maliciously altered.

An SSL certificate is a digital document issued by Certificate Authorities (CAs) and constitutes the heart of the SSL/TLS protocol. It authenticates a website's identity and includes information about it, such as the domain name and the site's owner relative information.

Mode of functioning of SSL certificates

The following figure illustrates the mode of functioning of the SSL certificates:

Mode of functioning of  the SSL certificates
Mode of functioning of the SSL certificates

SSL certificate errors can take a variety of forms, the most common ones are listed below:

  • SSL certificate not trusted error

  • Name mismatch error

  • Expired SSL certificate error

  • SSL certificate revoked error

Let's look at how to handle some of these errors in the following examples.

Example 1: Handling the SSL certificate revoked error:

This example shows how to seize an SSL certificate revoked error:

import os,time,logging
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
os.environ['WDM_PROGRESS_BAR'] = str(0)
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--no-sandbox')
chromeOptions.add_argument('--disable-gpu')
chromeOptions.add_argument('--headless')
chromeOptions.add_argument('--disable-dev-shm-usage')
chromeOptions.add_argument('--allow-running-insecure-content')
chromeOptions.add_argument('--ignore-certificate-errors')
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), chrome_options=chromeOptions)
driver.get('https://revoked.badssl.com/')
time.sleep(10)
print(driver.page_source)
driver.close()

Let's go through the above code widget:

  • Lines 1–5: Import the required Python libraries.

  • Line 7: Suppress the web driver default logging.

  • Lines 9–15: Initialize the Chrome web driver. We will see the following flags:

    • –allow-running-insecure-content: By default, Google Chrome blocks insecure content when visiting a secure page. This flag will allow insecure content.

    • --ignore-certificate-errors: This flag is used to avoid invalid certificate warning while testing using self-signed or invalid certificates.

  • Line 17–18: Launch the browser and fetch the specified URL.

  • Line 19: Suspend the execution for 10 seconds to wait for the specified URL to load.

  • Line 20: Display the source of the fetched URL.

  • Line 21: Close the browser window opened by the Selenium web driver.

Example 2: Handling the expired SSL certificate error:

This example illustrates how to grab an expired SSL certificate error:

import os,time,logging
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
os.environ['WDM_PROGRESS_BAR'] = str(0)
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--no-sandbox')
chromeOptions.add_argument('--disable-gpu')
chromeOptions.add_argument('--headless')
chromeOptions.add_argument('--disable-dev-shm-usage')
chromeOptions.add_argument('--allow-running-insecure-content')
chromeOptions.add_argument('--ignore-certificate-errors')
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), chrome_options=chromeOptions)
driver.get('https://expired.badssl.com/');
time.sleep(10)
print(driver.page_source)
driver.close()

The above code widget is similar to the one above it, except the fetched URL targets a website with an expired certificate.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved