我正在使用Selenium启动浏览器。如何处理要求浏览器接受证书的网页(URL)?
在Firefox中,我可能有一个类似的网站要求我接受这样的证书:
在Internet Explorer浏览器上,我可能会得到以下信息:
在谷歌浏览器上:
我重复我的问题:当我启动使用Selenium(Python编程语言)的浏览器(Internet Explorer,Firefox和Google Chrome)时,如何自动接受网站的证书?
Answers:
对于Firefox,您需要将accept_untrusted_certs
FirefoxProfile()
option设置为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
所需的功能:
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()
driver.get("javascript:document.getElementById('overridelink').click()")
allow-running-insecure-content
和ignore-certificate-errors
和allow-insecure-localhost
和unsafely-treat-insecure-origin-as-secure
(你可以尝试寻找更多:strings /opt/google/chrome/chrome | grep insecure
和类似grepping)
对于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自签名证书错误现已修复: 用木偶版firefox Webdrive python splinter接受ssl cert
“ 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")
对于通过python硒来解决与无头铬相关的问题的人,您可能会发现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)
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和上述解决方案不起作用,您可以尝试下面的(我原来的答复是代码在这里)。
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.DEFAULT_PREFERENCES['frozen']['marionette.contentListener'] = True
profile.DEFAULT_PREFERENCES['frozen']['network.stricttransportsecurity.preloadlist'] = False
profile.DEFAULT_PREFERENCES['frozen']['security.cert_pinning.enforcement_level'] = 0
profile.set_preference('webdriver_assume_untrusted_issuer', False)
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", temp_folder)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk",
"text/plain, image/png")
driver = webdriver.Firefox(firefox_profile=profile)
只是有关此问题的更新。
需要驱动程序:
Linux: Centos 7 64bit, Window 7 64bit
Firefox: 52.0.3
Selenium Webdriver: 3.4.0 (Windows), 3.8.1 (Linux Centos)
GeckoDriver: v0.16.0 (Windows), v0.17.0 (Linux Centos)
码
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();
我能够使用带有硒Web驱动程序3.1的PhantomJSDriver在.net c#上执行此操作
[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);
}
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("--ignore-certificate-errors");
driver = new ChromeDriver(options);
我已经将其用于Java和Chrome浏览器,效果很好
似乎仍然没有关于此问题的标准决定。换句话说,无论您是Internet Explorer,Mozilla还是Google Chrome,您仍然不能说“好的,请进行认证”。但是我发现了一篇帖子,展示了如何在Mozilla Firefox中解决该问题。如果您对此感兴趣,可以在此处进行检查。