Selenium Webdriver ConnectionAbortedError 在 5 秒的情况下在命令之间休眠
Selenium Webdriver ConnectionAbortedError in case of 5sec sleeps in between commands
我的 Selenium-WebDriver + Python 脚本工作正常,但如果 webdriver 命令之间的时间太长,请给一个 ConnectionAbortedError
。
下面的最小工作示例给出了一个错误:
from selenium import webdriver
from selenium.webdriver import Firefox
import time
browser = webdriver.Firefox()
browser.get('http://www.google.com/')
searchfield = browser.find_element_by_id("lst-ib")
time.sleep(5)
browser.close() # -> ConnectionAbortedError
虽然没有 5 秒睡眠,但没有错误:
browser = webdriver.Firefox()
browser.get('http://www.google.com/')
searchfield = browser.find_element_by_id("lst-ib")
browser.close()
其他人似乎没有遇到过这个问题...几秒钟后连接断开是否正常?还是我做错了什么?
我试过使用implicitly_wait和set_script_timeout,但是增加这些超时并没有解决问题。
ConnectionAbortedError
您看到 ConnectionAbortedError 作为 keep-alive connection
的默认超时是 5s.
根据@andreastt 的 comment in the discussion Keep-Alive connection to geckodriver 0.21.0 dropped after 5s of inactivity without re-connection using Selenium Python client GekcoDriver v0.21.0 打开使用 HTTP/1.1 Keep-Alive 连接.
正如您提到的 ConnectionAbortedError 如果在 webdriver 命令之间有 5 秒的休眠 由 @whimboo 的 comment 确认,他提到:
It looks like the default timeout for the keep-alive connection is 5s. Sadly once this time passed-by the connection is not correctly re-instantiated.
此处引用Why 5s?
GeckoDriver 团队 可能不得不将此超时提高到一个合理的值,但另一方面,客户端也必须在出现故障时创建一个新连接。预期是客户端需要在使用之前检查连接是否仍然存在。当连接在闲置五秒后最终被服务器关闭时,客户端需要建立一个新连接。尽管增加 Keep-Alive 超时 持续时间很有意义。因为这是 自动化测试人员 等待 五秒 操作完成或等待 五秒线程休眠的常见做法 元素在测试中为 present/visible。
但是再次强调,将 Keep-Alive connection timeout
上限设置为更高的值并不能解决根本问题,因为在 Support keep alive connections.
中提到的 WebDriver 客户端处理 HTTP 连接时存在错误
@AutomatedTester 进一步提到:
This issue is not because we are not connected at the time of doing a request. If that was the issue we would be getting a httplib.HTTPConnection
exception being thrown. Instead a BadStatusLine
is thrown when we do a connection and close it and try parse the response.
Now this could be the python stdlib bug, httplib bug or selenium bug, which will need investigating.
@andreastt 补充说:
The HTTPD’s Keep-Alive
timeout value is orthogonal to this issue. It is a known issue that the Python 2.7 standard library that urllib, used by the Selenium Python client, does not conform to HTTP/1.1. Increasing the server timeout would mitigate this, but not resolve the underlying problem, which is that the HTTP library in Python has a defect.
The issue appears to be fixed in more recent Python versions. When we investigated this we also found that various HTTP libraries built on top of urllib, such as requests, works around the issue using various mechanisms (like special-casing the BadStatusLine
exception and re-connecting).
Selenium Team was working on a patch for the Python client to replace urllib with something else that does not exhibit the same defect with Keep-Alive
connections. This work can be tracked in Urllib3.
Meanwhile the geckodriver team is working on extending the server-side timeout value to something more reasonable. As I said, this would help mitigate this issue but not fundamentally fix it. In any case it is true that five seconds is probably too low to get real benefit from persistent HTTP connections, and that increasing it to something like 60 seconds would have greater performance.
您可以在讨论 Increase Keep-Alive connection drop timeout.
中跟踪关于增加服务器 Keep-Alive
超时的工作
解决方案
升级您的测试环境以使用Selenium v3.14.0可能会解决您的问题。
我的 Selenium-WebDriver + Python 脚本工作正常,但如果 webdriver 命令之间的时间太长,请给一个 ConnectionAbortedError
。
下面的最小工作示例给出了一个错误:
from selenium import webdriver
from selenium.webdriver import Firefox
import time
browser = webdriver.Firefox()
browser.get('http://www.google.com/')
searchfield = browser.find_element_by_id("lst-ib")
time.sleep(5)
browser.close() # -> ConnectionAbortedError
虽然没有 5 秒睡眠,但没有错误:
browser = webdriver.Firefox()
browser.get('http://www.google.com/')
searchfield = browser.find_element_by_id("lst-ib")
browser.close()
其他人似乎没有遇到过这个问题...几秒钟后连接断开是否正常?还是我做错了什么?
我试过使用implicitly_wait和set_script_timeout,但是增加这些超时并没有解决问题。
ConnectionAbortedError
您看到 ConnectionAbortedError 作为 keep-alive connection
的默认超时是 5s.
根据@andreastt 的 comment in the discussion Keep-Alive connection to geckodriver 0.21.0 dropped after 5s of inactivity without re-connection using Selenium Python client GekcoDriver v0.21.0 打开使用 HTTP/1.1 Keep-Alive 连接.
正如您提到的 ConnectionAbortedError 如果在 webdriver 命令之间有 5 秒的休眠 由 @whimboo 的 comment 确认,他提到:
It looks like the default timeout for the keep-alive connection is 5s. Sadly once this time passed-by the connection is not correctly re-instantiated.
此处引用Why 5s?
GeckoDriver 团队 可能不得不将此超时提高到一个合理的值,但另一方面,客户端也必须在出现故障时创建一个新连接。预期是客户端需要在使用之前检查连接是否仍然存在。当连接在闲置五秒后最终被服务器关闭时,客户端需要建立一个新连接。尽管增加 Keep-Alive 超时 持续时间很有意义。因为这是 自动化测试人员 等待 五秒 操作完成或等待 五秒线程休眠的常见做法 元素在测试中为 present/visible。
但是再次强调,将 Keep-Alive connection timeout
上限设置为更高的值并不能解决根本问题,因为在 Support keep alive connections.
@AutomatedTester 进一步提到:
This issue is not because we are not connected at the time of doing a request. If that was the issue we would be getting a
httplib.HTTPConnection
exception being thrown. Instead aBadStatusLine
is thrown when we do a connection and close it and try parse the response. Now this could be the python stdlib bug, httplib bug or selenium bug, which will need investigating.
@andreastt 补充说:
The HTTPD’s
Keep-Alive
timeout value is orthogonal to this issue. It is a known issue that the Python 2.7 standard library that urllib, used by the Selenium Python client, does not conform to HTTP/1.1. Increasing the server timeout would mitigate this, but not resolve the underlying problem, which is that the HTTP library in Python has a defect.The issue appears to be fixed in more recent Python versions. When we investigated this we also found that various HTTP libraries built on top of urllib, such as requests, works around the issue using various mechanisms (like special-casing the
BadStatusLine
exception and re-connecting).Selenium Team was working on a patch for the Python client to replace urllib with something else that does not exhibit the same defect with
Keep-Alive
connections. This work can be tracked in Urllib3.Meanwhile the geckodriver team is working on extending the server-side timeout value to something more reasonable. As I said, this would help mitigate this issue but not fundamentally fix it. In any case it is true that five seconds is probably too low to get real benefit from persistent HTTP connections, and that increasing it to something like 60 seconds would have greater performance.
您可以在讨论 Increase Keep-Alive connection drop timeout.
中跟踪关于增加服务器Keep-Alive
超时的工作
解决方案
升级您的测试环境以使用Selenium v3.14.0可能会解决您的问题。