ConnectionResetError: [WinError 10054] 一个现有的连接被 ChromeDriver Chrome Selenium Django 的远程主机错误强行关闭

AAA*_*XXX 7 python django selenium google-chrome selenium-chromedriver

我正在通过“遵守测试山羊书”学习 TDD,但我正在尝试使用 Django 3。

如果有人知道的话,我在第6章。

我的代码是:

class VisitorTest(LiveServerTestCase):

    def setUp(self):
        self.browser = webdriver.Chrome()
        self.browser.implicitly_wait(2)

    def tearDown(self):
        self.browser.quit()

    def test_starting(self):
        print(self.live_server_url) 

        self.browser.get(self.live_server_url)
Run Code Online (Sandbox Code Playgroud)

在控制台我得到


Creating test database for alias 'default'...
System check identified no issues (0 silenced).

DevTools listening on ws://127.0.0.1:52187/devtools/browser/e9a03a04-819e-40a3-a0e4-bd4133d8f6cb
http://localhost:52180
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 52204)
----------------------------------------
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 52202)
Exception happened during processing of request from ('127.0.0.1', 52203)
Traceback (most recent call last):
Traceback (most recent call last):
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 654, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 654, in process_request_thread
    self.finish_request(request, client_address)
Traceback (most recent call last):
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 364, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 364, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 724, in __init__
    self.handle()
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 724, in __init__
    self.handle()
  File "C:\Users\Alex\PycharmProjects\goat\lib\site-packages\django\core\servers\basehttp.py", line 172, in handle
    self.handle_one_request()
  File "C:\Users\Alex\PycharmProjects\goat\lib\site-packages\django\core\servers\basehttp.py", line 172, in handle
    self.handle_one_request()
  File "C:\Users\Alex\PycharmProjects\goat\lib\site-packages\django\core\servers\basehttp.py", line 182, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "C:\Users\Alex\PycharmProjects\goat\lib\site-packages\django\core\servers\basehttp.py", line 182, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
----------------------------------------
----------------------------------------
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 654, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 364, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 724, in __init__
    self.handle()
  File "C:\Users\Alex\PycharmProjects\goat\lib\site-packages\django\core\servers\basehttp.py", line 174, in handle
    self.handle_one_request()
  File "C:\Users\Alex\PycharmProjects\goat\lib\site-packages\django\core\servers\basehttp.py", line 182, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
----------------------------------------
.
----------------------------------------------------------------------
Ran 1 test in 8.779s

OK
Destroying test database for alias 'default'...

Run Code Online (Sandbox Code Playgroud)

但是测试运行良好(我没有在这里显示它们)。

我进行了研究,但没有发现任何结论

知道为什么要显示这些以及如何修复它们吗?

Deb*_*anB 1

这个错误信息...

ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
Run Code Online (Sandbox Code Playgroud)

...意味着ChromeDriver浏览上下文(即Chrome 浏览器会话)之间的连接间歇性关闭。

当您使用的二进制版本之间不兼容时,就会出现此问题。


解决方案

确保这件事:

  • Selenium升级到当前级别Version 3.141.59
  • ChromeDriver已更新至当前ChromeDriver v79.0.3945.36级别。
  • Chrome已更新至当前Chrome 版本 79.0级别。(根据ChromeDriver v79.0 发行说明
  • 通过IDE清理项目工作区并仅使用所需的依赖项重建项目。
  • 仅限 WindowsOS)使用CCleaner工具清除执行测试套件之前和之后的所有操作系统杂务。
  • 仅限 LinuxOS)在执行测试套件之前和之后释放 Ubuntu/Linux Mint 中未使用/缓存的内存
  • 如果您的基本Web 客户端版本太旧,请将其卸载并安装最新的 GA 和发布版本的Web 客户端
  • 重新启动系统
  • @Test非 root用户身份执行。
  • driver.quit()始终在方法内调用tearDown(){}以优雅地关闭和销毁WebDriverWeb Client实例。

参考

您可以在以下位置找到一些相关讨论: