如何使用Selenium处理证书? [英] How to deal with certificates using Selenium?

查看:44
本文介绍了如何使用Selenium处理证书?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ChromeOptions() 参数:

from selenium import webdriver选项 = webdriver.ChromeOptions()options.add_argument('忽略证书错误')驱动程序 = webdriver.Chrome(chrome_options=options)driver.get('https://cacert.org/')驱动程序关闭()

对于 Internet Explorer,您需要设置 acceptSslCerts 想要的能力:

from selenium import webdriver能力 = webdriver.DesiredCapabilities().INTERNETEXPLORER能力['acceptSslCerts'] = True驱动程序 = webdriver.Ie(capabilities=capabilities)driver.get('https://cacert.org/')驱动程序关闭()

<小时>

实际上,根据 Desired Capabilities 文档,将 acceptSslCerts 功能设置为 True 应该适用于所有浏览器,因为它是通用读/写功能:

<块引用>

接受SslCerts

布尔值

会话是否应接受所有 SSL 证书默认情况下.

<小时>

Firefox 的工作演示:

<预><代码>>>>从硒导入网络驱动程序

acceptSslCerts 设置为 False:

<预><代码>>>>能力 = webdriver.DesiredCapabilities().FIREFOX>>>能力['acceptSslCerts'] = False>>>驱动程序 = webdriver.Firefox(能力=能力)>>>driver.get('https://cacert.org/')>>>打印(驱动程序.标题)不受信任的连接>>>驱动程序关闭()

acceptSslCerts 设置为 True:

<预><代码>>>>能力 = webdriver.DesiredCapabilities().FIREFOX>>>能力['acceptSslCerts'] = True>>>驱动程序 = webdriver.Firefox(能力=能力)>>>driver.get('https://cacert.org/')>>>打印(驱动程序.标题)欢迎来到 CAcert.org>>>驱动程序关闭()

I am using Selenium to launch a browser. How can I deal with the webpages (URLs) that will ask the browser to accept a certificate or not?

In Firefox, I may have a website like that asks me to accept its certificate like this:

On the Internet Explorer browser, I may get something like this:

On Google Chrome:

I repeat my question: How can I automate the acceptance of a website's certificate when I launch a browser (Internet Explorer, Firefox and Google Chrome) with Selenium (Python programming language)?

解决方案

For the Firefox, you need to set accept_untrusted_certs FirefoxProfile() option to 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()

For Chrome, you need to add --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()

For the Internet Explorer, you need to set 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()

这篇关于如何使用Selenium处理证书?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆