一尘不染

如何使用Selenium?处理证书Selenium?

selenium

我使用 Selenium
启动浏览器。如何处理要求浏览器接受证书的网页(URL)?

我重复我的问题: 当我启动 使用Selenium(Python编程语言)的浏览器(Internet Explorer,Firefox和Google Chrome)时,如何自动接受网站的证书?


阅读 604

收藏
2020-06-26

共1个答案

一尘不染

对于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() argument:

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
desired capability:

from selenium import webdriver

capabilities = webdriver.DesiredCapabilities().INTERNETEXPLORER
capabilities['acceptSslCerts'] = True

driver = webdriver.Ie(capabilities=capabilities)
driver.get('https://cacert.org/')

driver.close()

Actually, according to the Desired Capabilities
documentation
,
setting acceptSslCerts capability to True should work for all browsers
since it is a generic read/write capability:

acceptSslCerts

boolean

Whether the session should accept all SSL certs by default.


Working demo for Firefox:

>>> from selenium import webdriver

Setting acceptSslCerts to 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()

Setting acceptSslCerts to 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()
2020-06-26