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

Selenium重连已有浏览器报错:目标机器主动拒绝连接

Selenium重连持久化浏览器失败报错解决方法

2020年我可创建浏览器并通过主动抛异常防止执行后关闭,还能重连已有浏览器会话。今年旧方法失效后改用detach参数保持浏览器打开,却触发如下错误:

urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=61117): Max retries exceeded with url: /session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x034689E8>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

我的代码分为两步:

步骤1:创建持久化浏览器

from selenium import webdriver
import UtilityFunctions
from selenium.webdriver.chrome.options import Options

# STEP 1, CREATE PERSISTENT BROWSER
executable_path = UtilityFunctions.get_pycharm_root_folder() + "\\Selenium Drivers\\chromedriver.exe"
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(executable_path, chrome_options=chrome_options)
driver.get("http://www.yahoo.com")
session_url = driver.command_executor._url
session_id = driver.session_id
print(session_url)  # 复制到步骤2使用
print(session_id)  # 复制到步骤2使用

步骤2:重连已创建的浏览器

from selenium import webdriver
import UtilityFunctions
from selenium.webdriver.chrome.options import Options

# STEP 2, RECONNECT TO BROWSER CREATED IN STEP 1
session_id = "f173a51638c3aca3fd56a95b4df303f1"  # 从步骤1输出复制
session_url = "http://localhost:61117"  # 从步骤1输出复制
driver = webdriver.Remote(command_executor=session_url, desired_capabilities={})
driver.session_id = session_id
driver.get("http://www.google.com")

问题原因及解决办法

  • 问题核心:使用detach=True时,ChromeDriver会在浏览器启动后立即退出,导致原会话通信端口关闭,后续重连请求被拒绝。

  • 正确配置方案:改用远程调试端口+用户数据目录的组合,同时保持ChromeDriver通信链路可用:

    1. 修改步骤1代码:
from selenium import webdriver
import UtilityFunctions
from selenium.webdriver.chrome.options import Options

executable_path = UtilityFunctions.get_pycharm_root_folder() + "\\Selenium Drivers\\chromedriver.exe"
chrome_options = Options()
# 指定固定远程调试端口,确保后续可连接
chrome_options.add_argument("--remote-debugging-port=9222")
# 指定用户数据目录,保存浏览器会话状态
chrome_options.add_argument(r"--user-data-dir=C:\Temp\ChromeProfile")

# 新版本Selenium请用options参数替代chrome_options
driver = webdriver.Chrome(executable_path, options=chrome_options)
driver.get("http://www.yahoo.com")
print("调试地址: http://localhost:9222")
print("会话ID:", driver.session_id)
  1. 调整重连代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
# 直接通过调试端口连接已运行的浏览器
chrome_options.add_experimental_option("debuggerAddress", "localhost:9222")
driver = webdriver.Chrome(options=chrome_options)
driver.get("http://www.google.com")
  • 注意事项:
    • 确保user-data-dir指定的目录存在,否则Chrome会生成临时目录,导致会话无法复用。
    • 运行步骤1后,不要手动关闭ChromeDriver进程,确保9222端口处于监听状态。
    • Selenium 4.x+版本中,webdriver.Chrome不再支持chrome_options参数,统一改用options。

内容的提问来源于stack exchange,提问作者Al A'scgh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 19:30:54