selenium.common.exceptions.InsecureCertificateException: probably due to HSTS policy

Question:

I am trying to open microsoft outlook using selenium firefox, but I get this when the url opens
Connection to outlook.live.com has a security policy called HTTP Strict Transport Security (HSTS), which means that Firefox can only connect to it securely. The error I get on terminal is

Traceback (most recent call last):
  File "d:CodePythonplugins-olek-testinitializer.py", line 313, in start_driver
    driver.get(url)
  File "C:UsersIT PLANETAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremotewebdriver.py", line 449, in get
    self.execute(Command.GET, {"url": url})
  File "C:UsersIT PLANETAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremotewebdriver.py", line 440, in execute       
    self.error_handler.check_response(response)
  File "C:UsersIT PLANETAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremoteerrorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InsecureCertificateException: Message:
Stacktrace:
RemoteError@chrome://remote/content/shared/RemoteError.sys.mjs:8:8
WebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:180:5
InsecureCertificateError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:301:5
checkReadyState@chrome://remote/content/marionette/navigate.sys.mjs:58:24
onNavigation@chrome://remote/content/marionette/navigate.sys.mjs:329:39
emit@resource://gre/modules/EventEmitter.sys.mjs:154:20
receiveMessage@chrome://remote/content/marionette/actors/MarionetteEventsParent.sys.mjs:35:25

I have tried everything I could find on internet but none solved my problem. The code is

from seleniumwire import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.firefox import GeckoDriverManager
from webdriver_manager import utils
import sys

URL = 'https://outlook.live.com/'
firefox_options = webdriver.FirefoxOptions()
# firefox_options.add_argument('--no-sandbox')
path_to_firefox_profile = "output_files\firefox\xlycfcyp.default-release"
profile = webdriver.FirefoxProfile(path_to_firefox_profile)
profile.set_preference("dom.webdriver.enabled", False)
profile.set_preference('useAutomationExtension', False)
profile.update_preferences()
firefox_options.set_preference("general.useragent.override", 'user-agent=Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0')
firefox_options.add_argument("--width=1400")
firefox_options.add_argument("--height=1000")
driver_installation = GeckoDriverManager().install()
service = Service(driver_installation)
if sys.platform == 'win32':
    from subprocess import CREATE_NO_WINDOW
    service.creationflags = CREATE_NO_WINDOW

driver = webdriver.Firefox(options=firefox_options, firefox_profile=profile,
                            service=service)

driver.get(URL)

I have tried my best, I hope someone helps me.
Note:
The same thing works for gmail. It also has HSTS policy but it is working fine.
Thanks in advance

Asked By: farhan jatt

||

Answers:

Set profile attribute accept_untrusted_certs to True as follows:

profile = webdriver.FirefoxProfile(path_to_firefox_profile)
profile.accept_untrusted_certs = True

Update

Additionally, you can also add set the following preferences through an instance of firefox.options as follows:

from selenium.webdriver.firefox.options import Options

options=Options()
options.set_preference("security.mixed_content.block_active_content", false);
options.set_preference("security.mixed_content.block_display_content", true);
Answered By: undetected Selenium

I have found the solution and don’t know the exact reason why this worked. The fix was to set the following preference.

profile.set_preference("security.mixed_content.block_active_content", False)

This tells Firefox to allow mixed content (content that is loaded over both HTTP and HTTPS) on the website.
After this I did not get the Insecure Certificate Error.

Answered By: farhan jatt