我正在尝试使用带有 ChromeDriver 的 selenium 在本地主机(没有 HTTPS)上运行集成测试。
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
arg:
{'_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
?
最佳答案
这个错误信息...
This site can’t provide a secure connection app sent an invalid response. ERR_SSL_PROTOCOL_ERROR
...表示 ChromeDriver 无法在您的本地主机上启动/生成新的 WebBrowser,即 Chrome 浏览器 session 。
根据 this comment,您在本地主机(没有 HTTPS) 上看到这个问题盲目解决方案是通过 chromeOptions()
添加 argument
--allow-insecure-localhost
作为如下:
'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.
注意:此列表并不详尽。
从新 session 返回功能时,返回更具体的平台名称是有效的,允许用户正确识别运行 WebDriver 实现的操作系统。
因此,不是在 desiredCapabilities 对象中传递 "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
有关 ChromeDriver、Chrome 和 Selenium Client 版本的更多信息可以帮助我们更好地分析问题。然而,根据 ChromeDriver 历史记录,以下与处理证书错误 相关的问题已在 ChromeDriver 的最近几个版本中得到解决:
- Allow handling certificate errors via DevTools :由于 headless chrome 无法显示 SSL 证书错误的 UI 警告 fix已发布以将错误公开为 DevTools 事件并控制通过 DevTools 命令采取的操作。
- Provide ability to handle certificate errors in Chromedriver/Selenium for headless : 较早的某些security related options在 Chromium 的 UI 版本中通过 CLI 开关控制的(如
--ignore-certificate-errors
)被默默地忽略并且只能通过 devtools 设置。因此,有必要在浏览器目标 DevTools 客户端上覆盖和处理certificateError
事件。 fix已发布,实现了使用新的 DevTools 方法来覆盖浏览器范围内的证书错误处理,这也允许在 headless 模式下忽略证书错误。 - Global certificate error handling via DevTools :以前 DevTools 允许处理单个目标/WebContents 的证书错误,但是当创建新目标时(例如单击 target=_blank 链接),通常不可能发送
Security.enable
/Security.setOverrideCertificateErrors
命令在尝试导航之前足够快。 fix以更简单的“忽略所有证书错误”模式发布,而不是弃用旧的覆盖命令,支持新的setIgnoreCertificateErrors
命令,该命令还在浏览器目标上公开安全域,以促进全局应用此覆盖整个浏览器。
结论
- 确保添加以下参数/功能:
--allow-insecure-localhost
acceptInsecureCerts
--忽略证书错误
- 当您使用
'chromedriverVersion': '74.0.3729.6'
时,请确保您也在使用'chrome': '74.0'
(根据 ChromeDriver v74.0.3729.6 发布备注) - 确保您使用的是最新发布的 Selenium v3.141.59 客户端。
关于python - ChromeDriver ERR_SSL_PROTOCOL_ERROR 尽管 --ignore-certificate-errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56457778/