【问题标题】:ChromeDriver ERR_SSL_PROTOCOL_ERROR despite --ignore-certificate-errorsChromeDriver ERR_SSL_PROTOCOL_ERROR 尽管 --ignore-certificate-errors
【发布时间】:2019-10-20 19:17:56
【问题描述】:

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

Chrome 需要 https 证书,但从this 问题我了解到我可以使用 arg --ignore-certificate-errors 来规避这个问题

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

chromedriver 的反应还是出乎我的意料:

此网站无法提供安全连接 应用发送了无效响应。 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

【问题讨论】:

    标签: python selenium google-chrome selenium-chromedriver selenium-grid


    【解决方案1】:

    根据Fix "Aw, Snap!" page crashes and other page loading errors - Computer - Google Chrome Help(展开“页面加载错误代码和问题”部分),Chrome 会为任何与 SSL 相关的错误提供ERR_SSL_PROTOCOL_ERROR这包括:

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

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

    嗅探数据包,例如Wireshark 可以显示连接的初始阶段,包括协商阶段;如果服务器是你的(所以你有它的私钥),你也可以decrypt the encrypted parts——这会给你完整的画面。

    【讨论】:

    • 非常感谢您的回复,我不是连接参数协商和协议违规方面的专家,但我们在使用 firefox 时不会遇到这些问题,如果这是相关的话。我会仔细阅读您提到的问题
    • 如果不是证书错误,很可能是 TLS 版本或加密不匹配。 Chrome 可能会比 Firefox 更快地放弃旧标准支持。
    • 如果 Firefox 可以正常打开它,那么也没有违反协议 - 或者它也会打印错误,例如SSL_ERROR_RE_RECORD_TOO_LONG。
    【解决方案2】:

    此错误消息...

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

    ...暗示 ChromeDriver 无法在您的本地主机上启动/生成新的 WebBrowserChrome 浏览器 会话。 p>

    当您在 本地主机(没有 HTTPS) 上看到此问题时,根据 this comment,盲折叠解决方案是添加 argument --allow-insecure-localhost strong> 通过chromeOptions() 如下:

    '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 部分提到,以下平台名称是常用的,具有易于理解的语义,并且在匹配功能时,可以通过将它们视为有效的同义词来实现最大的互操作性对于知名操作系统:

    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.
    

    注意:此列表并不详尽。

    从新会话返回功能时,返回更具体的平台名称是有效的,允许用户正确识别运行 WebDriver 实现的操作系统。

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

    您可以在Curl error thrown for http POST to /session with params: {“desiredCapabilities”:{“browserName”:“chrome”,“platform”:“ANY” with Selenium and PHPUnit找到相关和相关的讨论


    更多关于 ChromeDriverChromeSelenium Client vrsion 的信息将有助于我们以更好的方式分析问题。然而,根据 ChromeDriver 的历史记录,以下与处理 证书错误 相关的问题已在 ChromeDriver 的最后几个版本中得到解决:

    • Allow handling certificate errors via DevTools:由于无头 chrome 无法针对 SSL 证书错误显示 UI 警告,因此发布了 fix 以将错误公开为 DevTools 事件并通过 DevTools 命令控制要采取的操作。
    • Provide ability to handle certificate errors in Chromedriver/Selenium for headless:之前在 Chromium 的 UI 版本中通过 CLI 开关控制的某些 security related options(如 --ignore-certificate-errors)被静默忽略,只能通过 devtools 设置。因此有必要在浏览器目标 DevTools 客户端上覆盖和处理 certificateError 事件。发布了fix,它实现了使用新的 DevTools 方法来覆盖浏览器范围内的证书错误处理,这也允许在无头模式下忽略证书错误。
    • Global certificate error handling via DevTools:以前 DevTools 允许处理单个目标/WebContents 的证书错误,但是当创建新目标时(例如单击 target=_blank 链接),通常无法发送 Security.enable / Security.setOverrideCertificateErrors在尝试导航之前足够快的命令。 fix 以更简单的“忽略所有证书错误”模式发布,而不推荐使用旧的覆盖命令,取而代之的是新的 setIgnoreCertificateErrors 命令,该命令还公开了浏览器目标上的安全域,以方便在全局范围内应用此覆盖浏览器。

    结论

    • 确保添加了以下参数/功能:
      • --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 客户端。

    【讨论】:

    • 反应热烈,似乎运作良好-非常感谢您的彻底回答
    【解决方案3】:

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

    这会导致 TLS/SSL 协商失败。

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

    使用--ignore-certificate-errors 选项,您可以生成自签名证书并将其应用于网络服务器。

    然后将 url 行更改为使用HTTPS

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

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2017-06-27
        • 1970-01-01
        • 1970-01-01
        • 2014-08-01
        • 2014-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-13
        相关资源
        最近更新 更多