如何处理使用selenium的证书?
我正在使用Selenium来启动浏览器。 如何处理要求浏览器接受证书的网页(URL)?
在Firefox中,我可能会有这样一个网站要求我接受这样的证书:
在Internet Explorer浏览器上,我可能会得到如下所示的内容:
在Google Chrome上:
我重复我的问题: 当我用Selenium(Python编程语言)启动浏览器(Internet Explorer,Firefox和Google Chrome)时,如何自动接受网站证书 ?
对于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
所需的function:
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
function设置为True
应该适用于所有浏览器,因为它是通用的读/写function:
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()
对于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);
对于Firefox Python:
Firefox自签名证书的bug现在已经被修复了: 接受ssl证书和marionette firefox webdrive python splinter
“acceptSslCerts”应replace为“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")
使用Javascript:
const capabilities = webdriver.Capabilities.phantomjs(); capabilities.set(webdriver.Capability.ACCEPT_SSL_CERTS, true); capabilities.set(webdriver.Capability.SECURE_SSL, false); capabilities.set('phantomjs.cli.args', ['--web-security=no', '--ssl-protocol=any', '--ignore-ssl-errors=yes']); const driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome(), capabilities).build();
从浏览器的证书存储中删除所有必需的证书,然后将浏览器configuration为在只有一个证书时自动select证书。
创build一个configuration文件,然后驱动程序可以帮助我们解决在Firefox中的证书问题:
var profile = new FirefoxProfile(); profile.SetPreference("network.automatic-ntlm-auth.trusted-uris","DESIREDURL"); driver = new FirefoxDriver(profile);
它看起来还没有这个问题的标准决定。 换句话说,如果你是Internet Explorer,Mozilla或谷歌浏览器,你仍然不能说“好的,做一个authentication。 但是我发现了一篇文章,展示了如何在Mozilla Firefox中解决这个问题。 如果你有兴趣,你可以在这里查看 。