在手动打开的现有浏览器中执行自动化时控制台报错求助
问题分析与解决
报错信息(中文翻译)
无法启动新会话。响应码500。消息:会话创建失败:无法连接到localhost:9183上的Chrome,原因:Chrome不可达
解决方案
要连接手动打开的Chrome执行自动化,核心是确保Chrome开启远程调试且代码配置匹配,按以下步骤排查:
正确启动带远程调试的Chrome
先关闭所有Chrome进程,再通过命令行启动,必须指定远程调试端口和独立用户数据目录(避免与默认配置冲突):- Windows:
chrome.exe --remote-debugging-port=9183 --user-data-dir="C:\ChromeDebugProfile" - Mac:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9183 --user-data-dir="/tmp/ChromeDebugProfile" - Linux:
google-chrome --remote-debugging-port=9183 --user-data-dir="/tmp/ChromeDebugProfile"
- Windows:
代码配置匹配远程调试端口
以Selenium为例,需在ChromeOptions中指定debugger地址,确保端口与启动时一致:from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.debugger_address = "localhost:9183" driver = webdriver.Chrome(options=options)验证端口可用性
执行curl http://localhost:9183/json/version测试,能返回JSON数据说明端口正常;若不通,检查防火墙是否拦截9183端口,或Chrome是否在该端口监听。确保Chrome与Driver版本一致
确认ChromeDriver版本和当前Chrome浏览器版本完全匹配,版本不兼容会导致连接失败。
内容的提问来源于stack exchange,提问作者Arjun Bhise
相关产品推荐
相关产品推荐

