首次使用Selenium:Chrome For Testing无法加载页面且驱动连接失败
Selenium Chrome驱动连接问题排查方案
问题背景
- 系统:MacOS Sonoma 14.4.1
- Chrome版本:125.0.6422.113(arm64官方版)
- ChromeDriver版本:125.0.6422.78
- 现象:
- 指定Chrome For Testing的二进制路径时,浏览器能启动,但无法加载
driver.get("https://www.facebook.com"),也不执行driver.quit(),30秒后抛出:selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service /path/to/chromedriver - 使用
/usr/local/bin路径时,报错:selenium.common.exceptions.WebDriverException: Message: Service (path) Status Code: -6
- 指定Chrome For Testing的二进制路径时,浏览器能启动,但无法加载
- 已尝试:重启电脑、重装ChromeDriver和Selenium
排查步骤
1. 严格匹配ChromeDriver与Chrome版本
ChromeDriver必须和Chrome的主版本号(前三位:125.0.6422)完全一致。当前Chrome是125.0.6422.113,ChromeDriver是125.0.6422.78,建议下载对应Chrome版本的arm64驱动替换现有文件。
2. 修复ChromeDriver权限问题
MacOS可能限制未授权驱动运行:
- 终端执行
chmod +x /path/to/chromedriver,给驱动添加可执行权限 - 若弹出“无法打开,因为Apple无法检查其是否包含恶意软件”,进入
系统设置 > 隐私与安全性,找到对应提示点击“仍要打开”
3. 修正驱动与浏览器的启动关联
不要直接指定Chrome For Testing路径,让ChromeDriver自动匹配本地安装的Chrome:
- 将ChromeDriver移动到
/usr/local/bin目录(确保该目录在系统PATH中) - 使用简化代码启动:
from selenium import webdriver from selenium.webdriver.chrome.service import Service service = Service('/usr/local/bin/chromedriver') driver = webdriver.Chrome(service=service) driver.get("https://www.facebook.com") driver.quit()
4. 排查端口占用与防火墙拦截
- 检查ChromeDriver默认端口9515是否被占用:终端执行
lsof -i :9515,若有占用进程,用kill -9 <PID>杀掉 - 进入
系统设置 > 网络 > 防火墙,确保ChromeDriver和Chrome都被允许联网
5. 启用驱动调试日志定位问题
启动服务时添加日志参数,查看详细错误:
from selenium import webdriver from selenium.webdriver.chrome.service import Service service = Service( executable_path='/path/to/chromedriver', log_path='/tmp/chromedriver.log' ) driver = webdriver.Chrome(service=service) driver.get("https://www.facebook.com") driver.quit()
查看/tmp/chromedriver.log中的日志,定位连接失败的具体原因。
6. 尝试Selenium Manager自动管理驱动
Selenium 4.6+内置的Selenium Manager可自动下载匹配的ChromeDriver,无需手动配置:
from selenium import webdriver driver = webdriver.Chrome() driver.get("https://www.facebook.com") driver.quit()
内容的提问来源于stack exchange,提问作者edgarisnew
相关产品推荐
相关产品推荐

