【问题标题】:Python selenium ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote hostPython selenium ConnectionResetError: [WinError 10054] 现有连接被远程主机强行关闭
【发布时间】:2018-01-02 16:29:21
【问题描述】:

我正在使用 python 3.6 并使用最新版本的 chromedriver,我尝试使用旧版本的 chromedriver,但重新启动我的电脑时遇到了同样的问题,同样的问题。这是我运行以重现错误的代码:

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("https://google.com")

完全错误:

    driver.get("https://google.com")
  File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 268, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 254, in execute
    response = self.command_executor.execute(driver_command, params)
  File "C:\Python36\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 464, in execute
    return self._request(command_info[0], url, body=data)
  File "C:\Python36\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 488, in _request
    resp = self._conn.getresponse()
  File "C:\Python36\lib\http\client.py", line 1331, in getresponse
    response.begin()
  File "C:\Python36\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "C:\Python36\lib\http\client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\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

【问题讨论】:

  • 您是否发现了问题所在以及如何解决?有同样的错误
  • 不,很遗憾我没有:/
  • 在使用带有 WebDrvier 的 Python 3 时遇到同样的问题,运行与 Python 2 对应的代码几乎相同的代码。我在一个盒子上运行了大约 10 个 Chromdriver 实例,但是这个问题仅在使用 Python 3.8 时出现,并且当实例数达到大约 8 个时似乎会发生,然后突然所有的 chrome 实例都被关闭并且新的实例不能被生成。仍在试图找出问题所在。

标签: python python-3.x selenium selenium-chromedriver


【解决方案1】:

在 driver.get("https://google.com") 之前添加 time.sleep(3),这将修复您的错误。那么如果你像我一样,你会得到一个不同的错误。

【讨论】:

    【解决方案2】:

    上面的 Seth 和 Jack1990 的回答对我解决从 python 使用 IEDriverServer 的问题很有帮助。我确实尝试了 Adhithiya 的建议,但这对我的问题没有帮助。

    This GitHub site 对我很有帮助。要注意的部分是“必需的配置”。我首先遵循了这一点,但是,在声明中,“在 Windows Vista 或 Windows 7 上的 IE 7 或更高版本上,您必须将每个区域的保护模式设置设置为相同的值。该值可以是打开或关闭,如只要每个区域都相同。”我发现我也必须为 Windows 10 执行此操作。事实上,python 错误信息在这一点上非常清楚。它们都需要启用或禁用。它们不必处于同一级别。

    另外,我确实必须在 time.sleep(x) 中处理 x 的值。这个睡眠命令是下面代码中driver = webdriver.Ie()driver.get("http://testwisely.com/demo") 之间的命令。如果对我来说设置为 5,ie 驱动程序首先会触发本地主机并抱怨无法访问它,然后它会连接到我想要它访问的页面(大部分时间!)。

    好消息是其他 3 个网络浏览器运行良好!我发现为 Chrome、Firefox 和 Edge(在 Windows 10 中)webdrivers 运行 driver.quit() 命令成功关闭了这些浏览器,而 iedriver 版本没有关闭 IE。

    下面是我的代码,以防你想用它来做实验。

    from selenium import webdriver
    import time
    
    browser_to_use = "Edge" # "Chrome" "Firefox" "Ie" 
    
    if browser_to_use == "Chrome":
        driver = webdriver.Chrome()
    elif browser_to_use == "Firefox":
        driver = webdriver.Firefox()
    elif browser_to_use == "Ie":  # This sucks!
        driver = webdriver.Ie()
        time.sleep(5)
    elif browser_to_use == "Edge":
        driver = webdriver.Edge()
    
    driver.get("http://testwisely.com/demo")
    time.sleep(5)
    driver.quit()
    

    【讨论】:

      【解决方案3】:

      Chromedriver 可能正在后台运行,请在任务管理器中检查后台进程

      如果您发现运行多个 chromedriver 实例,请手动终止所有进程并尝试再次运行该程序。

      你应该很高兴。

      【讨论】:

        【解决方案4】:

        下载旧版本(3.8)也可以解决问题,但测试运行速度会非常慢...

        你可以在这里找到链接:http://selenium-release.storage.googleapis.com/index.html?path=3.8/

        【讨论】:

          猜你喜欢
          • 2016-10-20
          • 2020-04-20
          • 2015-02-28
          • 2015-05-24
          • 1970-01-01
          • 2023-03-03
          • 2020-10-09
          • 1970-01-01
          • 2020-07-24
          相关资源
          最近更新 更多