You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium出现ConnectionResetError[WinError10054]错误求助

问题解决:Selenium循环执行时ConnectionResetError错误

问题原因

你的代码核心问题在于循环内关闭浏览器后,继续复用已失效的driver实例:

  • 第一次循环执行driver.close()后,浏览器窗口关闭,driver与浏览器的连接已断开
  • 下一次循环调用driver.get()时,Selenium尝试使用已断开的连接,触发ConnectionResetError
  • 另外路径字符串未加r前缀,可能存在转义问题(比如\P会被解析为转义字符)

解决方案

方案1:复用浏览器,刷新页面(推荐)

只初始化一次浏览器,循环内用refresh()刷新页面,避免频繁创建/销毁浏览器实例:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep as s

options = Options()
# 用r前缀定义路径,避免转义问题
options.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

# 仅初始化一次浏览器
driver = webdriver.Chrome(options=options, executable_path=r'chromedriver.exe')
driver.get('https://advertropolis.g1gabytedev.repl.co/jumbo.php')

try:
    while True:
        s(3)
        driver.refresh()  # 刷新当前页面
except KeyboardInterrupt:
    # 按下Ctrl+C时优雅关闭浏览器
    driver.quit()

方案2:每次循环重新创建浏览器实例

如果必须每次重新打开页面,需在循环内初始化driver,用完后用quit()彻底关闭:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep as s

options = Options()
options.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

try:
    while True:
        # 每次循环创建新的driver实例
        driver = webdriver.Chrome(options=options, executable_path=r'chromedriver.exe')
        driver.get('https://advertropolis.g1gabytedev.repl.co/jumbo.php')
        s(3)
        driver.quit()  # 彻底关闭浏览器和驱动连接
except KeyboardInterrupt:
    pass

额外注意事项

  • 确保Chrome浏览器版本与chromedriver版本严格匹配,版本不一致也可能导致连接问题
  • 优先使用driver.quit()而非driver.close(),quit()会彻底终止驱动进程,避免残留连接

内容的提问来源于stack exchange,提问作者teraswag

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 11:03:31