Python爬虫使用chromedriver模拟浏览器时提示cannot connect to chrome at 127.0.0.1:9222如何解决
源码 selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:9222
通过 chrome.exe --remote-debugging-post=9222 能启动,但是进不了 http://127.0.0.1:9222/和 http://127.0.0.1:9222/json ……
网上找了也没有什么实际解决办法,想问问 v 友们有没有什么办法……
Python爬虫使用chromedriver模拟浏览器时提示cannot connect to chrome at 127.0.0.1:9222如何解决
port。。。
这个问题我遇到过,是Chrome远程调试端口没配置对。
关键点在于启动Chrome时要指定远程调试端口,并且确保Selenium连接的是同一个端口。
解决方案:
-
先手动启动Chrome(指定调试端口)
在终端执行(Windows在CMD或PowerShell):"C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 --user-data-dir="C:\selenium\ChromeProfile"(Mac/Linux把路径换成你的Chrome安装位置)
-
Python代码连接已打开的浏览器
from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222") driver = webdriver.Chrome(options=chrome_options) print(driver.title) # 测试是否连接成功
注意:
- 如果9222端口被占用,换一个端口号(比如9223),两边保持一致就行。
--user-data-dir要指定一个新目录,避免和默认浏览器冲突。
一句话总结: 确保启动命令和代码里的调试端口一致。
。。。动手解决问题的能力越来越差 。。。
chromedriver 跟 chrome 有版本对应关系的,
你看看是不是一致.
手动写爬虫吧,遇到问题好解决
不懂 chromedriver,不过我看了一天 puppeteer 的中文文档,写了个爬虫,目前本地和服务器都稳定跑着,自动切换代理 IP 什么的,都没问题
看看 chrome 版本和 chromedriver 版本是否对应
可能是因为在 chrome 浏览器未关闭的情况下,就执行 chrome.exe --remote-debugging-port=9222 这个命令,要去任务管理器里查看是否有 chrome 进程的留存

