When using Selenium for automated testing, handling HTTPS websites or accepting untrusted SSL connections is a common task. This typically involves configuring the browser driver to trust all SSL certificates, even if they are self-signed or unrecognized. Below, I will detail how to configure these settings in some commonly used browsers:
1. Chrome browser
For Chrome, you can configure ChromeOptions to accept untrusted SSL certificates. Here is an example of how to set it up:
pythonfrom selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument('--ignore-certificate-errors') # Ignore certificate errors driver = webdriver.Chrome(executable_path='path_to_chromedriver', options=chrome_options) driver.get('https://example.com')
This argument instructs Chrome to ignore SSL certificate errors.
2. Firefox browser
For Firefox, you can configure FirefoxProfile to accept untrusted SSL certificates.
pythonfrom selenium import webdriver from selenium.webdriver.firefox.options import Options as FirefoxOptions firefox_options = FirefoxOptions() firefox_profile = webdriver.FirefoxProfile() firefox_profile.accept_untrusted_certs = True # Accept untrusted certificates driver = webdriver.Firefox(executable_path='path_to_geckodriver', firefox_profile=firefox_profile, options=firefox_options) driver.get('https://example.com')
In this example, setting accept_untrusted_certs to True indicates that Firefox should accept all untrusted certificates.
3. Edge browser
For Edge, the approach is similar to Chrome since both are based on Chromium.
pythonfrom selenium import webdriver from selenium.webdriver.edge.options import Options edge_options = Options() edge_options.add_argument('--ignore-certificate-errors') # Ignore certificate errors driver = webdriver.Edge(executable_path='path_to_edgedriver', options=edge_options) driver.get('https://example.com')
Through these examples, you can configure the respective settings for Chrome, Firefox, or Edge to accept untrusted SSL certificates, ensuring Selenium accesses HTTPS websites smoothly even with problematic SSL certificates. This is particularly useful in testing environments, as they often use self-signed certificates.