【问题标题】:How to deal with certificates using Selenium?如何使用 Selenium 处理证书?
【发布时间】:2014-08-21 19:48:26
【问题描述】:

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

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

在 Internet Explorer 浏览器上,我可能会得到如下信息:

在谷歌浏览器上:

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

【问题讨论】:

    标签: python selenium python-3.x selenium-webdriver browser


    【解决方案1】:

    对于 .NET,对我有用的是以下内容...

    var chromeOptions = new ChromeOptions { AcceptInsecureCertificates = true };
    

    几乎是,它告诉 ChromeDriver 选项在检测到不安全的证书时不要停止浏览器执行,并正常进行。

    【讨论】:

      【解决方案2】:
          ChromeOptions options = new ChromeOptions().addArguments("--proxy-server=http://" + proxy);
          options.setAcceptInsecureCerts(true);
      

      【讨论】:

      • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因
      【解决方案3】:

      对于 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 所需的功能:

      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 documentation,将acceptSslCerts 能力设置为True 应该适用于所有浏览器,因为它是一种通用的读/写能力:

      接受SslCerts

      布尔值

      会话是否应接受所有 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()
      

      【讨论】:

      • 我无法让它在 IE 11 上运行,它只是不断向我显示证书错误页面
      • 对于使用 geckodriver 的 firefox 48+ 仍然有问题,这是 geckodriver 中的未解决问题,他们仍然不知道,请参阅Bug Issue
      • 此答案不再有效,请改用“acceptInsecureCerts”
      • 这条评论可能很晚,但对现在解决问题的人很有帮助。我尝试了以上所有方法,但没有任何效果。只设法通过错误:driver.get("javascript:document.getElementById('overridelink').click()")
      • 对于 chromedriver,我最终将所有这四个字符串传递给 options.add_argument --> allow-running-insecure-contentignore-certificate-errorsallow-insecure-localhostunsafely-treat-insecure-origin-as-secure (您可以尝试通过以下方式找到更多信息: strings /opt/google/chrome/chrome | grep insecure 和类似的grepping)
      【解决方案4】:
      WebDriverManager.chromedriver().setup();
      ChromeOptions options = new ChromeOptions();
      options.addArguments("--ignore-certificate-errors");
      driver = new ChromeDriver(options);
      

      我已经将它用于 Java 和 Chrome 浏览器,它运行良好

      【讨论】:

      • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请编辑您的答案以添加解释并说明适用的限制和假设。
      【解决方案5】:

      我遇到了完全相同的问题。但是,当我尝试在浏览器中手动打开网站时,证书是正确的,但在详细信息中,名称是“DONOTTRUST”。

      证书的差异是由于 Fiddler 在后台运行并在重新加密之前解密所有 HTTPS 内容造成的。

      要解决我的问题,只需关闭机器上的 Fiddler。如果您需要保持 Fiddler 处于打开状态,则可以在 Fiddler 设置中取消选中解密 SSL。

      【讨论】:

        【解决方案6】:

        我在使用 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
        

        【讨论】:

          【解决方案7】:

          每当我在使用较新的浏览器时遇到此问题时,我只需使用 AppRobotic 个人版单击特定的屏幕坐标,或通过按钮切换并单击。

          基本上它只是使用它的宏功能,但不适用于无头设置。

          【讨论】:

            【解决方案8】:

            在 C#(.net 核心)中使用 Selenium.WebdriverSelenium.Chrome.Webdriver 像这样:

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

            【讨论】:

              【解决方案9】:

              对于那些使用Firefox来解决这个问题并且上面的解决方案都不起作用的人,你可以试试下面的代码(我的原始答案是here)。

              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)
              

              【讨论】:

                【解决方案10】:

                我可以在 .net c# 上使用 PhantomJSDriver 和 selenium web driver 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);
                    }
                

                【讨论】:

                  【解决方案11】:

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

                  desired_capabilities = {
                      "acceptInsecureCerts": True
                  }
                  

                  【讨论】:

                    【解决方案12】:

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

                    需要驱动程序:

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

                    【讨论】:

                      【解决方案13】:

                      对于通过 python selenium 提出与 headless 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)
                      

                      【讨论】:

                        【解决方案14】:

                        对于 Firefox Python:

                        Firefox 自签名证书错误现已修复: accept ssl cert with marionette firefox webdrive python splinter

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

                        【讨论】:

                        • 现在 Firefox 52 上线了。升级Firefox,升级selenium到v3.3,下载geckodriver到v0.15,你甚至不再需要二进制路径了!
                        【解决方案15】:

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

                        【讨论】:

                          【解决方案16】:

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

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

                          【讨论】:

                          • Internet Explorer 和 Google Chrome 怎么样?
                          【解决方案17】:

                          对于火狐:

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

                          【讨论】:

                          • 问题是关于 Python 的。你至少可以写出那是什么语言。
                          • 小心,'ProfilesIni' 已被弃用!
                          • 希望java版可以帮助ChromeOptions options = new ChromeOptions(); options .addArguments("--ignore-ssl-errors=yes", "--ignore-certificate-errors"); ChromeDriver driver = new ChromeDriver(options);
                          【解决方案18】:

                          看起来它仍然没有这个问题的标准决定。换句话说 - 你仍然不能说“好吧,做一个认证,不管你是 Internet Explorer、Mozilla 还是 Google Chrome”。但我发现一篇文章展示了如何解决 Mozilla Firefox 中的问题。有兴趣的可以关注here

                          【讨论】:

                          • 但是上面用 Java 完成的代码呢?它要求每个浏览器接受当前访问网站的证书。我们不能在 Python 中做同样的事情吗?
                          【解决方案19】:

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

                          【讨论】:

                            猜你喜欢
                            • 1970-01-01
                            • 2013-02-04
                            • 2016-03-23
                            • 2012-06-17
                            • 1970-01-01
                            • 2011-08-21
                            • 2021-05-12
                            • 1970-01-01
                            • 2015-06-10
                            相关资源
                            最近更新 更多