問題描述
我正在嘗試使用 selenium 和 ChromeDriver 在本地主機(沒有 HTTPS)上運行集成測試.
Chrome 需要 https 證書,但從 this 問題中我了解到我可以使用 arg --ignore-certificate-errors
我還增加了我的能力 acceptInsecureCerts
,因為這似乎是適當的行動方案(docs)
chromedriver 的反應還是出乎我的意料:
<塊引用>此網站無法提供安全連接 應用發送了無效響應.ERR_SSL_PROTOCOL_ERROR
我的代碼如下:
從 selenium 導入 webdriver從 selenium.webdriver.chrome.options 導入選項# 制作選項(主要是忽略證書)選項 = webdriver.ChromeOptions()options.add_argument('--ignore-certificate-errors')# 添加acceptInsecureCerts能力 = options.to_capabilities()能力['acceptInsecureCerts'] = Trueprint(capabilities) # 見下文驅動程序 = webdriver.Remote(command_executor=SELENIUM_HUB,所需的能力=能力)print(driver.__dict__) # 見下文app_login_url = 'http://app:8000/accounts/login/'driver.get(app_login_url)
我的能力:
{'acceptInsecureCerts': True,瀏覽器名稱":鉻",'goog:chromeOptions': {'args': ['--ignore-certificate-errors'],'擴展':[]},平臺":任何",'版本': ''}
這是我的驅動程序信息,似乎只考慮了 acceptInsecureCerts
參數:
{'_file_detector': <selenium.webdriver.remote.file_detector.LocalFileDetector object at 0x7fb42bde10f0>,'_is_remote':是的,'_mobile': <selenium.webdriver.remote.mobile.Mobile 對象在 0x7fb42bb5e400>,'_switch_to': <selenium.webdriver.remote.switch_to.SwitchTo 對象在 0x7fb42bdd4898>,'功能':{'acceptInsecureCerts':真,'acceptSslCerts':是的,'applicationCacheEnabled':假,'browserConnectionEnabled':假,瀏覽器名稱":鉻",'chrome': {'chromedriverVersion': '74.0.3729.6''(255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29})','userDataDir': '/tmp/.com.google.Chrome.vc1ZvB'},'cssSelectorsEnabled':真,'databaseEnabled':假,'goog:chromeOptions': {'debuggerAddress': 'localhost:40815'},'handlesAlerts':是的,'hasTouchScreen':錯誤,'javascriptEnabled':是的,'locationContextEnabled':真,'mobileEmulationEnabled':假,'nativeEvents':是的,'networkConnectionEnabled':假,'pageLoadStrategy': '正常',平臺":Linux",'代理人': {},可旋轉":錯誤,'setWindowRect':真,'strictFileInteractability':假,'takesHeapSnapshot':是的,'takesScreenshot':是的,'超時':{'隱式':0,頁面加載":300000,'腳本': 30000},'unexpectedAlertBehaviour': '忽略','版本': '74.0.3729.169','webStorageEnabled':真,'webdriver.remote.sessionid': '1cf77f237e966bac6ca15d4d9c107423'},'command_executor': <selenium.webdriver.remote.remote_connection.RemoteConnection 對象在 0x7fb42be0cf98>,'error_handler': <selenium.webdriver.remote.errorhandler.ErrorHandler 對象在 0x7fb427d08a20>,'session_id': '1cf77f237e966bac6ca15d4d9c107423','w3c':錯誤}
為什么我仍然看到 ERR_SSL_PROTOCOL_ERROR
?
這個錯誤信息...
此站點無法提供安全連接應用程序發送了無效響應.ERR_SSL_PROTOCOL_ERROR
...暗示 ChromeDriver 無法在您的本地主機上啟動/生成新的 WebBrowser 即 Chrome 瀏覽器 會話.p>
根據 this comment 一個盲目的解決方案是添加 argument
--allow-insecure-localhost代碼>
通過chromeOptions()
如下:
'goog:chromeOptions': {'args': ['--allow-insecure-localhost'],擴展":[]}
<小時>
但是,您的主要問題似乎在于您設置 platform
的 capabilities 被設置為 ANY
如下:
{'acceptInsecureCerts': True,瀏覽器名稱":鉻",'goog:chromeOptions': {'args': ['--ignore-certificate-errors'],'擴展':[]},平臺":任何",'版本': ''}
根據 WebDriver - W3C Living Document platformName 部分提到,以下平臺名稱是常用的,具有易于理解的語義,并且在匹配功能時,可以通過將它們視為知名操作系統的有效同義詞來實現最大的互操作性:
鑰匙系統--- ------linux" 任何基于 Linux 內核的服務器或桌面系統."mac" Apple macOS 的任何版本.windows" Microsoft Windows 的任何版本,包括桌面版和移動版.
注意:此列表并不詳盡.
從新會話返回功能時,返回更具體的平臺名稱是有效的,允許用戶正確識別運行 WebDriver 實現的操作系統.
因此,不是在 desiredCapabilities 對象中傳遞 "platform":"ANY"
,而是使用更具體的 "platform":"linux"
將是更可取的方法.
您可以在 帶有參數的 http POST 到/session 引發的 curl 錯誤:{desiredCapabilities":{browserName":chrome",platform":ANY" with Selenium 和 PHPUnit
<小時>
更多關于 ChromeDriver、Chrome 和 Selenium Client vrsion 的信息將有助于我們以更好的方式分析問題.然而,根據 ChromeDriver 的歷史記錄,以下與處理 證書錯誤 相關的問題已在 ChromeDriver 的最后幾個版本中得到解決:
- 允許通過 DevTools 處理證書錯誤:作為 headlesschrome 無法針對 SSL 證書錯誤顯示 UI 警告 修復 已發布以公開錯誤作為 DevTools 事件并通過 DevTools 命令控制要采取的操作.
- 提供在 Chromedriver/Selenium for headless 中處理證書錯誤的能力:之前通過 CLI 開關控制的某些 安全相關選項Chromium 的 UI 版本(如
--ignore-certificate-errors
)被靜默忽略,只能通過 devtools 設置.因此有必要在瀏覽器目標 DevTools 客戶端上覆蓋和處理certificateError
事件.修復已發布,實現了使用新的 DevTools 方法來覆蓋證書錯誤處理瀏覽器范圍內也允許在無頭模式下忽略證書錯誤. - 通過 DevTools 處理全局證書錯誤:以前允許使用 DevTools處理單個目標/WebContents 的證書錯誤,但是當創建新目標時(例如單擊 target=_blank 鏈接),通常不可能發送
Security.enable
/Security.setOverrideCertificateErrors
命令在嘗試導航之前足夠快.修復 發布了一個更簡單的忽略所有證書錯誤"模式,而不是棄用舊的覆蓋命令支持新的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 客戶端.
I'm trying to run integration tests on a local host (with no HTTPS) using selenium with ChromeDriver.
Chrome requires an https certificate, but from this question i understand that i can circumvent this using the arg --ignore-certificate-errors
I have also added to my capabilities acceptInsecureCerts
, as this seems like the appropriate course of action (docs)
The response from the chromedriver is still not what I was expecting:
This site can’t provide a secure connection app sent an invalid response. ERR_SSL_PROTOCOL_ERROR
My code is below:
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)
My capabilities:
{'acceptInsecureCerts': True,
'browserName': 'chrome',
'goog:chromeOptions': {'args': ['--ignore-certificate-errors'],
'extensions': []},
'platform': 'ANY',
'version': ''}
Here is my driver info, it looks like only the acceptInsecureCerts
arg has been taken into account:
{'_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}
Why am i still seeing the ERR_SSL_PROTOCOL_ERROR
?
This error message...
This site can’t provide a secure connection app sent an invalid response. ERR_SSL_PROTOCOL_ERROR
...implies that the ChromeDriver was unable to initiate/spawn a new WebBrowser i.e. Chrome Browser session on your localhost.
As you are seeing this issue on your local host (with no HTTPS) as per this comment a blind fold solution would be to add the argument
--allow-insecure-localhost
through chromeOptions()
as follows:
'goog:chromeOptions': {'args': ['--allow-insecure-localhost'],
'extensions': []}
However your main issue seems to be with the capabilities where you have set platform
being set s ANY
as follows:
{'acceptInsecureCerts': True,
'browserName': 'chrome',
'goog:chromeOptions': {'args': ['--ignore-certificate-errors'],
'extensions': []},
'platform': 'ANY',
'version': ''}
As per WebDriver - W3C Living Document the platformName section mentions, the following platform names are in common usage with well-understood semantics and, when matching capabilities, greatest interoperability can be achieved by honoring them as valid synonyms for well-known Operating Systems:
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.
Note:This list is not exhaustive.
When returning capabilities from New Session, it is valid to return a more specific platformName, allowing users to correctly identify the Operating System the WebDriver implementation is running on.
So instead of passing "platform":"ANY"
within the desiredCapabilities object, a more specific "platform":"linux"
will be more desirable approach.
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
Some more information about the ChromeDriver, Chrome and Selenium Client vrsion would have helped us to analyze the issue in a better way. However as per ChromeDriver history the following issues related to handling of certificate errors were addressed in the last few releases of ChromeDriver:
- Allow handling certificate errors via DevTools: As the headless chrome cannot show a UI warning for SSL certificate errors a fix was released to expose the errors as DevTools events and control the action to take through a DevTools command.
- 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 handlecertificateError
events on the browser-target DevTools client. A fix was released implementing the usage of the new DevTools method to override certificate error handling browser-wide which allowed ignoring certificate errors in headless mode too. - 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 was published with a simpler "ignore all cert errors" mode instead deprecated the old override command in favor of a newsetIgnoreCertificateErrors
command which also exposes the Security domain on the browser target to facilitate applying this override globally for the whole browser.
Conclusion
- Ensure that the following arguments/capabilities are added:
--allow-insecure-localhost
acceptInsecureCerts
--ignore-certificate-errors
- As you are using
'chromedriverVersion': '74.0.3729.6'
ensure that you are also using'chrome': '74.0'
(as per ChromeDriver v74.0.3729.6 Release Notes) - Ensure that you are using the latest released Selenium v3.141.59 clients.
這篇關于ChromeDriver ERR_SSL_PROTOCOL_ERROR 盡管 --ignore-certificate-errors的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!