python selenium 元素,常见,异常,不安全证书异常:可能是由于HSTS政策

bgibtngc  于 2023-01-24  发布在  Python
关注(0)|答案(1)|浏览(243)

我尝试使用Selenium Firefox打开Microsoft Outlook,但当URL打开时出现此问题连接到outlook.live.com有一个名为HTTP严格传输安全(HSTS)的安全策略,这意味着Firefox只能安全地连接到它。

Traceback (most recent call last):
  File "d:\Code\Python\plugins-olek-test\initializer.py", line 313, in start_driver
    driver.get(url)
  File "C:\Users\IT PLANET\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 449, in get
    self.execute(Command.GET, {"url": url})
  File "C:\Users\IT PLANET\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute       
    self.error_handler.check_response(response)
  File "C:\Users\IT PLANET\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.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

我已经尝试了一切我可以找到在互联网上,但没有解决我的问题。代码是

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)

我已经尽力了,我希望有人能帮助我。**注:**同样的事情适用于gmail。它也有HSTS政策,但它运行良好

vuktfyat

vuktfyat1#

将配置文件属性accept_untrusted_certs设置为***True***,如下所示:

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

相关问题