如何在Selenium中处理证书?

117 投票
20 回答
214643 浏览
提问于 2025-04-18 11:42

我正在使用Selenium来启动一个浏览器。我该如何处理那些会要求浏览器接受证书的网页(网址)呢?

在Firefox浏览器中,我可能会遇到这样的网站,它会要求我接受它的证书:

Firefox

在Internet Explorer浏览器中,我可能会看到类似这样的提示:

Enter image description here

在Google Chrome浏览器中:

Google Chrome

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

20 个回答

6

在C#(.net core)中,使用,可以这样做:

ChromeOptions options = new ChromeOptions();
options.AddArgument("--ignore-certificate-errors");
using (var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),options))
{ 
  ...
}
6

对于那些通过Python的Selenium使用无头Chrome的人来说,你可能会觉得这个链接很有用:https://bugs.chromium.org/p/chromium/issues/detail?id=721739#c102

看起来你可以选择以下两种方式之一:

chrome_options = Options()
chrome_options.add_argument('--allow-insecure-localhost')

或者可以尝试类似下面的方式(可能需要根据Python进行调整):

ChromeOptions options = new ChromeOptions()
DesiredCapabilities caps = DesiredCapabilities.chrome()
caps.setCapability(ChromeOptions.CAPABILITY, options)
caps.setCapability("acceptInsecureCerts", true)
WebDriver driver = new ChromeDriver(caps)
7

关于Firefox和Python:

Firefox中自签名证书的问题现在已经修复了:

如何在Python的Marionette Firefox WebDriver中接受SSL证书

这里提到的“acceptSslCerts”应该改成“acceptInsecureCerts”。

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

caps = DesiredCapabilities.FIREFOX.copy()
caps['acceptInsecureCerts'] = True
ff_binary = FirefoxBinary("path to the Nightly binary")

driver = webdriver.Firefox(firefox_binary=ff_binary, capabilities=caps)
driver.get("https://expired.badssl.com")
10

对于Firefox浏览器:

ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("default");
myprofile.setAcceptUntrustedCertificates(true);
myprofile.setAssumeUntrustedCertificateIssuer(true);
WebDriver driver = new FirefoxDriver(myprofile);

对于Chrome浏览器,我们可以使用:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--ignore-certificate-errors"));
driver = new ChromeDriver(capabilities);

对于Internet Explorer浏览器,我们可以使用:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);      
Webdriver driver = new InternetExplorerDriver(capabilities);
194

对于Firefox浏览器,你需要把 accept_untrusted_certsFirefoxProfile() 选项中设置为 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浏览器,你需要在 ChromeOptions() 中添加 --ignore-certificate-errors 参数:

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 的文档,把 acceptSslCerts 设置为 True 应该对所有浏览器都有效,因为这是一个通用的读写能力:

acceptSslCerts

布尔值

这个会话是否应该默认接受所有SSL证书。


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()

撰写回答