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.
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.
The following figure illustrates the 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.
This example shows how to seize an SSL certificate revoked error:
import os,time,loggingfrom selenium import webdriverfrom webdriver_manager.chrome import ChromeDriverManagerfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.service import Serviceos.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.
This example illustrates how to grab an expired SSL certificate error:
import os,time,loggingfrom selenium import webdriverfrom webdriver_manager.chrome import ChromeDriverManagerfrom selenium.webdriver.chrome.service import Serviceos.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