Python 如何使用Selenium处理证书?
我正在使用启动浏览器。如何处理要求浏览器接受或不接受证书的网页(URL) 在Firefox中,我可能会有这样一个网站,要求我接受它的证书,如下所示:Python 如何使用Selenium处理证书?,python,selenium,python-3.x,selenium-webdriver,browser,Python,Selenium,Python 3.x,Selenium Webdriver,Browser,我正在使用启动浏览器。如何处理要求浏览器接受或不接受证书的网页(URL) 在Firefox中,我可能会有这样一个网站,要求我接受它的证书,如下所示: ChromeOptions options = new ChromeOptions(); options.AddArgument("--ignore-certificate-errors"); using (var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecuti
ChromeOptions options = new ChromeOptions();
options.AddArgument("--ignore-certificate-errors");
using (var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),options))
{
...
}
在Internet Explorer浏览器上,我可能会得到如下内容:
ChromeOptions options = new ChromeOptions();
options.AddArgument("--ignore-certificate-errors");
using (var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),options))
{
...
}
在Google Chrome上:
我重复我的问题:当我使用Selenium(Python编程语言)启动浏览器(Internet Explorer、Firefox和Google Chrome)时,如何自动接受网站的证书??看起来它仍然没有解决这个问题的标准决定。换句话说,你仍然不能说“好吧,做一个认证,不管你是InternetExplorer、Mozilla还是Google Chrome”。但我发现有一篇文章展示了如何在Mozilla Firefox中解决这个问题。如果您对它感兴趣,可以查看它。对于Firefox:
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("default");
myprofile.setAcceptUntrustedCertificates(true);
myprofile.setAssumeUntrustedCertificateIssuer(true);
WebDriver driver = new FirefoxDriver(myprofile);
>>> from selenium import webdriver
对于Chrome我们可以使用:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--ignore-certificate-errors"));
driver = new ChromeDriver(capabilities);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
Webdriver driver = new InternetExplorerDriver(capabilities);
对于InternetExplorer,我们可以使用:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--ignore-certificate-errors"));
driver = new ChromeDriver(capabilities);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
Webdriver driver = new InternetExplorerDriver(capabilities);
创建配置文件和驱动程序有助于解决Firefox中的证书问题:
var profile = new FirefoxProfile();
profile.SetPreference("network.automatic-ntlm-auth.trusted-uris","DESIREDURL");
driver = new FirefoxDriver(profile);
对于Firefox,您需要将
accept\u untrusted\u 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()
>>> 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()
对于Chrome,您需要添加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,您需要设置所需的功能:
from selenium import webdriver
capabilities = webdriver.DesiredCapabilities().INTERNETEXPLORER
capabilities['acceptSslCerts'] = True
driver = webdriver.Ie(capabilities=capabilities)
driver.get('https://cacert.org/')
driver.close()
实际上,根据,将
acceptSslCerts
功能设置为True
应该适用于所有浏览器,因为它是一种通用的读/写功能:
from selenium import webdriver
capabilities = webdriver.DesiredCapabilities().INTERNETEXPLORER
capabilities['acceptSslCerts'] = True
driver = webdriver.Ie(capabilities=capabilities)
driver.get('https://cacert.org/')
driver.close()
承兑人
布尔值
会话是否应接受所有SSL证书
默认情况下
Firefox的工作演示:
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("default");
myprofile.setAcceptUntrustedCertificates(true);
myprofile.setAssumeUntrustedCertificateIssuer(true);
WebDriver driver = new FirefoxDriver(myprofile);
>>> 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
:
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()
>>> 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()
从浏览器的证书存储中删除除必要证书以外的所有证书,然后将浏览器配置为仅存在一个证书时自动选择证书 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();
对于Firefox Python:
Firefox自签名证书错误现已修复:
“acceptSslCerts”应替换为“AcceptUnsecureCerts”
对于通过python selenium来回答与无头chrome相关的问题的人,您可能会发现这很有用 看起来你要么做,要么做
chrome_options = Options()
chrome_options.add_argument('--allow-insecure-localhost')
或者类似以下内容(可能需要适应python):
只是关于这个问题的更新 需要驱动程序:
Linux:Centos 7 64位,windows7 64位
Firefox:52.0.3
seleniumwebdriver:3.4.0(Windows)、3.8.1(Linux Centos)
GeckoDriver:v0.16.0(Windows)、v0.17.0(Linux Centos)
代码
在selenium python中,需要将
所需的\u功能设置为:
desired_capabilities = {
"acceptInsecureCerts": True
}
我能够在.NETC#上使用PhantomJSDriver和SeleniumWebDriver 3.1实现这一点
[TestMethod]
public void headless()
{
var driverService = PhantomJSDriverService.CreateDefaultService(@"C:\Driver\phantomjs\");
driverService.SuppressInitialDiagnosticInformation = true;
driverService.AddArgument("--web-security=no");
driverService.AddArgument("--ignore-ssl-errors=yes");
driver = new PhantomJSDriver(driverService);
driver.Navigate().GoToUrl("XXXXXX.aspx");
Thread.Sleep(6000);
}
对于那些使用Firefox来解决这个问题的人,如果上面的解决方案不起作用,你可以尝试下面的代码(我最初的答案是)
在C#(.net内核)中使用Selenium.Webdriver
和Selenium.Chrome.Webdriver
如下:
ChromeOptions options = new ChromeOptions();
options.AddArgument("--ignore-certificate-errors");
using (var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),options))
{
...
}
每当我在较新的浏览器中遇到这个问题时,我都会使用AppRobotic Personal edition来单击特定的屏幕坐标,或者通过按钮单击tab并单击
基本上,它只是使用它的宏功能,但在无头设置上不起作用 我遇到了与Selenium和Behat相同的问题。如果要通过behat.yml
传递参数,则需要如下所示:
default:
extensions:
Behat\MinkExtension:
base_url: https://my-app.com
default_session: selenium2
selenium2:
browser: firefox
capabilities:
extra_capabilities:
acceptInsecureCerts: true
我也有同样的问题。然而,当我尝试在浏览器中手动打开网站时,证书是正确的,但在细节中,名称是“DONOTTRUST”
证书的差异是由于在后台运行的Fiddler在重新加密之前解密了所有HTTPS内容造成的
要解决我的问题,只需关闭机器上的小提琴手。如果需要保持Fiddler处于打开状态,则可以在Fiddler设置中取消选中Decrypt SSL
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("--ignore-certificate-errors");
driver = new ChromeDriver(options);
我在Java和Chrome浏览器中使用过它,它在.NET中运行得很好,对我有效的是以下内容
ChromeOptions options = new ChromeOptions().addArguments("--proxy-server=http://" + proxy);
options.setAcceptInsecureCerts(true);
var chromeOptions = new ChromeOptions { AcceptInsecureCertificates = true };
基本上,它告诉ChromeDriver选项,当检测到不安全的证书时,不要停止浏览器执行,并正常进行。但是上面用Java完成的代码呢?它要求每个浏览器接受当前访问网站的证书。我们不能在Python中做同样的事情吗?Internet Explorer和Google Chrome呢?我不能让它在IE 11上工作,它只是不断向我显示firefox 48+使用geckodriver的证书错误页面仍然存在问题,这是geckodriver中的公开问题,他们仍然不知道,看这个答案不再有效,使用“AcceptUnsecureCerts”来代替此评论可能会很晚,但对现在提出问题的人很有帮助。我尝试了以上所有方法,但都没有效果。只能通过以下命令传递错误:driver.get(“javascript:document.getElementById('overridelink')。click()”)
对于chromedriver,我最终将这四个字符串全部传递给了选项。添加参数-->允许运行不安全的内容
和忽略证书错误
和允许不安全的本地主机
和不安全地将不安全的源视为安全的
(你可以通过:strings/opt/google/chrome/chrome | grep unsecure
和类似的grepping来尝试查找更多内容)问题是关于Python的。你至少可以编写那是什么语言。小心,“ProfilesIni”不推荐使用!希望java版本可以帮助ChromeOptions options=new ChromeOptions();options.addArguments(“--忽略ssl错误=是”,“忽略证书错误”);ChromeDriver驱动程序=新ChromeDriver(选项);A