Fix Firefox Insecure Connection Errors in Selenium WebDriver
In this article
When you run Selenium WebDriver tests against Firefox and hit an insecure connection error (like MOZILLA_PKIX_ERROR_MITM_DETECTED or SEC_ERROR_UNKNOWN_ISSUER), it usually means Firefox's certificate validation is failing, often due to a proxy or a self-signed certificate. This guide shows you how to configure Firefox so Selenium can proceed past these errors, either by disabling certificate checks or by trusting the specific certificate.
What You'll Need
- Python (or Java/JavaScript) with Selenium installed
- Firefox browser (any recent version)
- geckodriver (matching your Firefox version)
- Access to the website's certificate file (if you want to trust it explicitly)
Step-by-Step
- Identify the exact error. Run your Selenium script and note the error message in the browser's error page. Common ones include SEC_ERROR_UNKNOWN_ISSUER, MOZILLA_PKIX_ERROR_MITM_DETECTED, or SSL_ERROR_BAD_CERT_DOMAIN. This tells you whether the issue is an untrusted root, a self-signed cert, or a domain mismatch.
- Set Firefox preferences to accept insecure certificates. In your Selenium code, create a Firefox profile and set these preferences:
profile.set_preference("acceptInsecureCerts", True)This is the simplest fix—it tells Firefox to accept any certificate, including self-signed ones. For Python, it looks like:from selenium import webdriver from selenium.webdriver.firefox.options import Options from selenium.webdriver.firefox.service import Service options = Options() options.accept_insecure_certs = True driver = webdriver.Firefox(service=Service("/path/to/geckodriver"), options=options) - Alternatively, set the preference via Firefox options. In some Selenium versions, you can do:
options.set_preference("acceptInsecureCerts", True)This is equivalent to the previous step. - If the error persists, disable certificate validation entirely. This is more aggressive but works for any certificate issue. Add these preferences to your profile:
profile.set_preference("security.enterprise_roots.enabled", True) profile.set_preference("security.nocertdb", True)The first enables enterprise roots (useful for corporate proxies), and the second disables the certificate database, which can bypass some validation errors. - For proxy-related errors (MITM), configure Firefox to trust the proxy's certificate. If you're behind a corporate proxy that intercepts HTTPS, you need to add the proxy's root certificate to Firefox's trust store. Export the certificate from your proxy (usually via the browser's certificate manager or your IT department), then import it into your Selenium profile:
profile.set_preference("security.enterprise_roots.enabled", True)Then, in your test, use a custom Firefox profile with the certificate imported. You can do this by creating a profile manually in Firefox, importing the cert, and then pointing Selenium to that profile usingOptions.profile. - Use a custom Firefox profile with the certificate imported. If the above doesn't work, create a new Firefox profile manually:
- Close all Firefox windows.
- Open a terminal and run
firefox -Pto open the Profile Manager. - Create a new profile (e.g., "selenium").
- Launch Firefox with that profile, go to the site that gives the error, click "Advanced" → "Accept the Risk and Continue" to temporarily bypass.
- Then go to Settings → Privacy & Security → Certificates → View Certificates → Your Certificates, and import the proxy's certificate if needed.
- Alternatively, you can use the
certutilcommand-line tool to import the certificate into the profile's cert database. - In your Selenium code, use
Options.profile = "/path/to/profile"to load that profile.
- For domain mismatch errors, use the
set_preferencefornetwork.stricttransportsecurity.preloadlist. If the error is due to HSTS, you can disable it with:profile.set_preference("network.stricttransportsecurity.preloadlist", False) profile.set_preference("security.cert_pinning.enforcement_level", 0)This disables HSTS and certificate pinning, which can cause insecure connection errors on sites that have strict policies. - Run your test again. After applying the preferences, restart the driver and test. If you still get errors, check the browser's console (about:certerror) for more details.
Common Mistakes
- Forgetting to apply the profile to the driver. You must pass the
Optionsobject towebdriver.Firefox()—not just create it. - Using
acceptInsecureCertswith a proxy that requires enterprise roots. This alone may not work if the proxy's certificate is not trusted; you need to also enablesecurity.enterprise_roots.enabled. - Setting preferences after the driver is already running. Preferences must be set before the browser launches.
- Assuming the error is always a certificate issue. Sometimes it's a network issue—check your internet connection.
Tips
- Use
acceptInsecureCertsas your first line of defense—it's simple and works for most self-signed certs. - If you're testing against a staging environment with a self-signed cert, consider using a local CA and importing it into your profile for a more realistic test.
- Keep your geckodriver and Firefox versions in sync; mismatches can cause unexpected errors.
- If you're using a corporate proxy, ask your IT team for the root certificate—it's a common request.
Troubleshooting
- Error still appears after setting preferences. Make sure you're not using a cached profile. Delete the profile directory or use a fresh one.
- Firefox opens but the error page shows. Check if the preferences are actually applied by navigating to
about:configand searching foracceptInsecureCerts—it should betrue. - If you get a "certificate has expired" error, check the system date/time—it might be off.
- If you're using a remote WebDriver (like Selenium Grid), the preferences must be set on the node's Firefox instance, not your local machine.
- If the error is
MOZILLA_PKIX_ERROR_MITM_DETECTED, it's almost always a proxy issue—try the enterprise roots setting or import the proxy cert.
Frequently Asked Questions
Does acceptInsecureCerts work for all insecure connection errors?
It works for most certificate-related errors, but not for HSTS or pinning errors. You may need to also disable those features.
Is it safe to disable certificate checks in tests?
Yes, for testing purposes only. It doesn't affect your production browser or security settings.
Can I use a Firefox profile I already have?
Yes, but be careful—the profile might have other settings that interfere. It's better to create a dedicated test profile.
Why does my test pass locally but fail on a CI server?
CI servers often have different proxy or network configurations. Apply the same preferences in your CI test script.
How do I import a certificate into a Selenium Firefox profile?
Create a profile manually, import the cert via Firefox's UI or certutil, then use that profile in Selenium.
What if I'm using Firefox in headless mode?
The same preferences work in headless mode. Just set them before launching the driver.