ChromeDriver ERR_SSL_PROTOCOL_ERROR 尽管有 --ignore-certificate-errors

ChromeDriver ERR_SSL_PROTOCOL_ERROR despite --ignore-certificate-errors

我正在尝试 运行 在本地主机(没有 HTTPS)上使用带有 ChromeDriver 的 selenium 进行集成测试。

Chrome 需要 https 证书,但是从 this 问题我知道我可以使用 arg --ignore-certificate-errors

来绕过这个

我还增加了我的能力 acceptInsecureCerts,因为这似乎是适当的行动方案 (docs)

chromedriver 的响应仍然不是我所期望的:

This site can’t provide a secure connection app sent an invalid response. ERR_SSL_PROTOCOL_ERROR

我的代码如下:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# make options (principally to ignore certificate)
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')

# add acceptInsecureCerts
capabilities = options.to_capabilities()
capabilities['acceptInsecureCerts'] = True

print(capabilities) # see below

driver = webdriver.Remote(
    command_executor=SELENIUM_HUB,
    desired_capabilities=capabilities
)
print(driver.__dict__) # see further below

app_login_url = 'http://app:8000/accounts/login/'

driver.get(app_login_url)

我的能力:

{'acceptInsecureCerts': True,
'browserName': 'chrome',
'goog:chromeOptions': {'args': ['--ignore-certificate-errors'],
                        'extensions': []},
'platform': 'ANY',
'version': ''}

这是我的驱动程序信息,看起来只考虑了 acceptInsecureCerts 参数:

{'_file_detector': <selenium.webdriver.remote.file_detector.LocalFileDetector object at 0x7fb42bde10f0>,
'_is_remote': True,
'_mobile': <selenium.webdriver.remote.mobile.Mobile object at 0x7fb42bb5e400>,
'_switch_to': <selenium.webdriver.remote.switch_to.SwitchTo object at 0x7fb42bdd4898>,
'capabilities': {'acceptInsecureCerts': True,
                'acceptSslCerts': True,
                'applicationCacheEnabled': False,
                'browserConnectionEnabled': False,
                'browserName': 'chrome',
                'chrome': {'chromedriverVersion': '74.0.3729.6 '
                                                    '(255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29})',
                            'userDataDir': '/tmp/.com.google.Chrome.vc1ZvB'},
                'cssSelectorsEnabled': True,
                'databaseEnabled': False,
                'goog:chromeOptions': {'debuggerAddress': 'localhost:40815'},
                'handlesAlerts': True,
                'hasTouchScreen': False,
                'javascriptEnabled': True,
                'locationContextEnabled': True,
                'mobileEmulationEnabled': False,
                'nativeEvents': True,
                'networkConnectionEnabled': False,
                'pageLoadStrategy': 'normal',
                'platform': 'Linux',
                'proxy': {},
                'rotatable': False,
                'setWindowRect': True,
                'strictFileInteractability': False,
                'takesHeapSnapshot': True,
                'takesScreenshot': True,
                'timeouts': {'implicit': 0,
                            'pageLoad': 300000,
                            'script': 30000},
                'unexpectedAlertBehaviour': 'ignore',
                'version': '74.0.3729.169',
                'webStorageEnabled': True,
                'webdriver.remote.sessionid': '1cf77f237e966bac6ca15d4d9c107423'},
'command_executor': <selenium.webdriver.remote.remote_connection.RemoteConnection object at 0x7fb42be0cf98>,
'error_handler': <selenium.webdriver.remote.errorhandler.ErrorHandler object at 0x7fb427d08a20>,
'session_id': '1cf77f237e966bac6ca15d4d9c107423',
'w3c': False}

为什么我仍然看到 ERR_SSL_PROTOCOL_ERROR

根据 Fix "Aw, Snap!" page crashes and other page loading errors - Computer - Google Chrome Help(扩展 "Page loading error codes and issues" 部分),Chrome 对于任何 SSL-related 错误给出 ERR_SSL_PROTOCOL_ERROR 这包括:

  • 证书错误
  • 连接参数协商失败(例如要使用的 TLS 版本和流加密)
  • 同行违反协议

由于您无法从 Chrome 获取更多详细信息,因此在其他应用程序(例如 Firefox 或使用 openssl s_client)中打开该页面可以为您提供有关正在发生的事情的更多详细信息。

嗅探数据包,例如Wireshark can show the initial stages of the connection including the negotiation stage; if the server is yours (so you have its private key), you will also be able to decrypt the encrypted parts -- 这将为您提供全貌。

这个错误信息...

This site can’t provide a secure connection app sent an invalid response. ERR_SSL_PROTOCOL_ERROR

...表示 ChromeDriver 无法 initiate/spawn 新的 WebBrowserChrome 浏览器 session 在您的本地主机上。

正如您在 本地主机(没有 HTTPS) 上看到的这个问题,根据 this comment 盲目解决方案是添加 argument --allow-insecure-localhostchromeOptions()如下:

'goog:chromeOptions': {'args': ['--allow-insecure-localhost'],
            'extensions': []}

然而,您的主要问题似乎与 功能 相关,您已将 platform 设置为 ANY如下:

{'acceptInsecureCerts': True,
'browserName': 'chrome',
'goog:chromeOptions': {'args': ['--ignore-certificate-errors'],
            'extensions': []},
'platform': 'ANY',
'version': ''}

根据 WebDriver - W3C Living Document platformName 部分提到的,以下平台名称通常使用 well-understood 语义,并且在匹配功能时,最大的互操作性可以通过将它们作为 well-known 操作系统的有效同义词来实现:

Key         System
---         ------
"linux"     Any server or desktop system based upon the Linux kernel.
"mac"       Any version of Apple’s macOS.
"windows"   Any version of Microsoft Windows, including desktop and mobile versions.

注意:此列表并不详尽。

当 returning 新功能 Session 时,它对 return 更具体的平台名称有效,允许用户正确识别 Web 操作系统 Driver实施 运行 上。

因此,与其在 desiredCapabilities object 中传递 "platform":"ANY",不如采用更具体的 "platform":"linux" 方法。

You can find a relevant and related discussion in Curl error thrown for http POST to /session with params: {“desiredCapabilities”:{“browserName”:“chrome”,“platform”:“ANY” with Selenium and PHPUnit


关于 ChromeDriverChrome 和 [=58 的更多信息=]Selenium Client vrsion 可以帮助我们更好地分析问题。然而,根据 ChromeDriver 历史,以下与处理 证书错误 相关的问题在最近的几个版本中得到了解决ChromeDriver:

  • Allow handling certificate errors via DevTools: As the headless chrome cannot show a UI warning for SSL certificate errors a fix 的发布是为了将错误公开为 DevTools 事件,并控制通过 DevTools 命令采取的操作。
  • Provide ability to handle certificate errors in Chromedriver/Selenium for headless: Earlier certain security related options that was controlled via CLI switches in the UI version of Chromium (like --ignore-certificate-errors) were silently ignored and can only be set via devtools. So it was necessary to override and handle certificateError events on the browser-target DevTools client. A fix 已发布,实现了使用新的 DevTools 方法来覆盖证书错误处理 browser-wide,这也允许在无头模式下忽略证书错误。
  • Global certificate error handling via DevTools: Previously DevTools allowed handling certificate errors for individual targets / WebContents, but when a new target was created (e.g. clicking on a target=_blank link), it was not often not possible to send the Security.enable / Security.setOverrideCertificateErrors commands quickly enough before a navigation is attempted. A fix 以更简单的 "ignore all cert errors" 模式发布,而不是弃用旧的覆盖命令以支持新的 setIgnoreCertificateErrors 命令,该命令还在浏览器目标上公开安全域以促进为整个浏览器全局应用此覆盖。

结论

  • 确保添加以下 arguments/capabilities:
    • --allow-insecure-localhost
    • acceptInsecureCerts
    • --ignore-certificate-errors
  • 当您使用 'chromedriverVersion': '74.0.3729.6' 时,请确保您也在使用 'chrome': '74.0'(根据 ChromeDriver v74.0.3729.6 发行说明)
  • 确保您使用的是最新发布的 Selenium v​​3.141.59 客户端。

您是通过 HTTP 而不是 HTTPS 请求页面。 Chrome 不会连接到不安全的 HTTP 服务器。

这导致 TLS/SSL 协商失败。

您需要确保您的服务器 运行 HTTPS 在 TCP 端口 8000 上。

使用 --ignore-certificate-errors 选项,您可以生成 self-signed 证书并将其应用到 Web 服务器。

然后将 url 行更改为使用 HTTPS

app_login_url = 'https://app:8000/accounts/login/'

我在 MAC 遇到过这个问题,添加 chrome 选项没有解决问题。

以下解决方法为我解决了问题:

  • 导航到 /usr/local/lib/python/site-packages/seleniumwire/proxy
  • 双击ca.crt文件
  • Keychain Access 应用程序中双击 Selenium Wire CA 证书
  • 展开 Trust
  • Select Always Trust 下拉选项
  • 使用密码进行身份验证