对于Firefox,您需要将accept_untrusted_certs FirefoxProfile()选项设置为True:
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.accept_untrusted_certs = True
driver = webdriver.Firefox(firefox_profile=profile)
driver.get('https://cacert.org/')
driver.close()
对于Chrome,您需要添加--ignore-certificate-errors ChromeOptions()参数:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://cacert.org/')
driver.close()
对于Internet Explorer,需要设置acceptSslCerts所需的功能:
from selenium import webdriver
capabilities = webdriver.DesiredCapabilities().INTERNETEXPLORER
capabilities['acceptSslCerts'] = True
driver = webdriver.Ie(capabilities=capabilities)
driver.get('https://cacert.org/')
driver.close()
实际上,根据Desired Capabilities documentation,将acceptSslCerts能力设置为True应该适用于所有浏览器,因为它是一个通用的读/写能力:
acceptSslCerts
boolean
Whether the session should accept all SSL certs
by default.
Firefox的工作演示:
>>> from selenium import webdriver
将acceptSslCerts设置为False:
>>> capabilities = webdriver.DesiredCapabilities().FIREFOX
>>> capabilities['acceptSslCerts'] = False
>>> driver = webdriver.Firefox(capabilities=capabilities)
>>> driver.get('https://cacert.org/')
>>> print(driver.title)
Untrusted Connection
>>> driver.close()
将acceptSslCerts设置为True:
>>> capabilities = webdriver.DesiredCapabilities().FIREFOX
>>> capabilities['acceptSslCerts'] = True
>>> driver = webdriver.Firefox(capabilities=capabilities)
>>> driver.get('https://cacert.org/')
>>> print(driver.title)
Welcome to CAcert.org
>>> driver.close()