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

Selenium连接手动打开的Chrome报错:disconnected: not connected to DevTools

解决Selenium附加已打开Chrome时的"disconnected: not connected to DevTools"错误

问题描述

我编写了一个Selenium脚本用于自动化网站任务,需要将Selenium附加到已手动打开的浏览器上。我在Chrome快捷方式中添加了--remote-debugging-port=1234参数,并使用以下代码进行连接:

debug_port = 1234

options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", f"localhost:{debug_port}")

driver1 = webdriver.Chrome(options=options)

该脚本在我的计算机上运行完全正常,可像普通Selenium WebDriver一样控制浏览器。但在另一台用户的计算机上运行时,出现以下错误:

selenium.common.exceptions.WebDriverException: Message: disconnected: not connected to DevTools 

(failed to check if window was closed: disconnected: not connected to DevTools)

(Session info: chrome=117.0.5938.92)

我已在该用户的Chrome快捷方式中同样添加了--remote-debugging-port=1234参数,求解决建议?

解决建议

  • 确保Chrome完全关闭后再启动
    打开任务管理器结束所有chrome.exe后台进程,再双击修改后的快捷方式启动浏览器,避免旧进程占用端口或干扰调试连接。

  • 验证调试端口的监听状态
    打开命令提示符执行netstat -ano | findstr :1234,查看是否有进程占用1234端口,且进程ID与当前Chrome的PID一致。如果端口被占用,更换未被使用的端口(比如9222),同步修改代码和快捷方式中的参数。

  • 检查Chrome快捷方式参数格式
    右键快捷方式→属性,确认目标栏中chrome.exe路径后有空格,再添加--remote-debugging-port=1234,参数前不能被引号包裹,正确格式示例:
    "C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=1234

  • 确认Selenium与Chrome版本兼容
    检查用户电脑上的Selenium版本是否适配Chrome 117.0.5938.92,建议升级到Selenium 4.6+版本(自带ChromeDriver自动管理),执行pip install --upgrade selenium完成升级。

  • 排除安全软件干扰
    临时关闭杀毒软件或防火墙,测试脚本是否正常运行。若恢复正常,将Chrome或Python脚本添加到安全软件的信任列表。

  • 使用独立用户配置文件
    在Chrome快捷方式中额外添加--user-data-dir="C:\temp\ChromeProfile"参数,创建独立配置文件,避免原有扩展、插件或设置干扰调试连接。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 08:46:09