Python 如何使用 Selenium 处理证书?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24507078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:42:46  来源:igfitidea点击:

How to deal with certificates using Selenium?

pythonseleniumpython-3.xselenium-webdriverbrowser

提问by

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

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

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

在 Firefox 中,我可能有一个这样的网站,要求我接受它的证书,如下所示:

Firefox

火狐

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

在 Internet Explorer 浏览器上,我可能会得到这样的信息:

Enter image description here

在此处输入图片说明

On Google Chrome:

在谷歌浏览器上:

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

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

采纳答案by alecxe

For the Firefox, you need to set accept_untrusted_certsFirefoxProfile()option to True:

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

For Chrome, you need to add --ignore-certificate-errorsChromeOptions()argument:

对于 Chrome,您需要添加参数:--ignore-certificate-errorsChromeOptions()

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

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


Actually, according to the Desired Capabilitiesdocumentation, setting acceptSslCertscapability to Trueshould work for all browsers since it is a generic read/write capability:

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

acceptSslCerts

boolean

Whether the session should accept all SSL certs by default.

接受证书

布尔值

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



Working demo for Firefox:

Firefox 的工作演示:

>>> from selenium import webdriver

Setting acceptSslCertsto False:

设置acceptSslCertsFalse

>>> 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 acceptSslCertsto True:

设置acceptSslCertsTrue

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

回答by Stanimir Yakimov

It looks like it still doesn't have a standard decision of this problem. In other words - you still can't say "Okay, do a certification, whatever if you are Internet Explorer, Mozilla or Google Chrome". But I found one post that shows how to work around the problem in Mozilla Firefox. If you are interested in it, you can check it here.

看起来它仍然没有这个问题的标准决定。换句话说 - 你仍然不能说“好吧,做一个认证,不管你是 Internet Explorer、Mozilla 还是谷歌浏览器”。但我发现了一篇文章,展示了如何解决 Mozilla Firefox 中的问题。如果您对此感兴趣,可以在此处查看

回答by Susanta Adhikary

For Firefox:

对于火狐:

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

For Chromewe can use:

对于Chrome,我们可以使用:

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

For Internet Explorerwe can use:

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

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

回答by user2062360

Creating a profile and then a driver helps us get around the certificate issue in Firefox:

创建配置文件然后创建驱动程序可以帮助我们解决 Firefox 中的证书问题:

var profile = new FirefoxProfile();
profile.SetPreference("network.automatic-ntlm-auth.trusted-uris","DESIREDURL");
driver = new FirefoxDriver(profile);

回答by gracefulcode

Delete all but the necessary certificate from your browser's certificate store and then configure the browser to automatically select the certificate when only one certificate is present.

从浏览器的证书存储中删除除必要证书之外的所有证书,然后将浏览器配置为在只有一个证书时自动选择证书。

回答by Jamil Aryan

Javascript:

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

回答by Rémi Debette

For Firefox Python:

对于 Firefox Python:

The Firefox Self-signed certificate bug has now been fixed: accept ssl cert with marionette firefox webdrive python splinter

Firefox 自签名证书错误现已修复: 使用 marionette firefox webdrive python splinter 接受 ssl 证书

"acceptSslCerts" should be replaced by "acceptInsecureCerts"

“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")

回答by oxer

For people coming to this question related to headless chrome via python selenium, you may find https://bugs.chromium.org/p/chromium/issues/detail?id=721739#c102to be useful.

对于通过 python selenium 提出与无头 chrome 相关的这个问题的人,您可能会发现https://bugs.chromium.org/p/chromium/issues/detail?id=721739#c102很有用。

It looks like you can either do

看起来你可以做

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

or something along the lines of the following (may need to adapt for python):

或类似以下内容(可能需要适应 python):

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

回答by HA S

Just an update regarding this issue.

只是关于这个问题的更新。

Require Drivers:

需要驱动程序:

Linux: Centos 7 64bit, Window 7 64bit

Linux: Centos 7 64bit, Window 7 64bit

Firefox: 52.0.3

Firefox: 52.0.3

Selenium Webdriver: 3.4.0 (Windows), 3.8.1 (Linux Centos)

Selenium Webdriver: 3.4.0 (Windows), 3.8.1 (Linux Centos)

GeckoDriver: v0.16.0 (Windows), v0.17.0 (Linux Centos)

GeckoDriver: v0.16.0 (Windows), v0.17.0 (Linux Centos)

Code

代码

System.setProperty("webdriver.gecko.driver", "/home/seleniumproject/geckodrivers/linux/v0.17/geckodriver");

ProfilesIni ini = new ProfilesIni();


// Change the profile name to your own. The profile name can 
// be found under .mozilla folder ~/.mozilla/firefox/profile. 
// See you profile.ini for the default profile name

FirefoxProfile profile = ini.getProfile("default"); 

DesiredCapabilities cap = new DesiredCapabilities();
cap.setAcceptInsecureCerts(true);

FirefoxBinary firefoxBinary = new FirefoxBinary();

GeckoDriverService service =new GeckoDriverService.Builder(firefoxBinary)
    .usingDriverExecutable(new 
File("/home/seleniumproject/geckodrivers/linux/v0.17/geckodriver"))
    .usingAnyFreePort()
    .usingAnyFreePort()
    .build();
try {
    service.start();
} catch (IOException e) {
    e.printStackTrace();
}

FirefoxOptions options = new FirefoxOptions().setBinary(firefoxBinary).setProfile(profile).addCapabilities(cap);

driver = new FirefoxDriver(options);
driver.get("https://www.google.com");

System.out.println("Life Title -> " + driver.getTitle());
driver.close();

回答by nattster

In selenium python, you need to set desired_capabilitiesas:

在 selenium python 中,您需要设置desired_capabilities为:

desired_capabilities = {
    "acceptInsecureCerts": True
}