使用Chrome配置文件启动Selenium ChromeDriver失败求助
解决Selenium指定Chrome配置文件启动失败的问题
针对你遇到的Chrome无法启动、报错unknown error: Chrome failed to start: exited normally的问题,按以下步骤排查修复:
1. 确保Chrome进程完全终止
如果已有Chrome实例在使用目标配置文件,Selenium无法重复加载该配置。打开任务管理器结束所有chrome.exe进程,或执行命令强制终止:
taskkill /F /IM chrome.exe
2. 校验配置文件路径与权限
- 确认
chrome_profile_path指向的User Data目录真实存在,且Profile 2文件夹确实位于该目录下 - 改用原始字符串避免路径转义异常,修改路径定义:
chrome_profile_path = r'C:\Users\tan.le\AppData\Local\Google\Chrome\User Data' - 检查该目录的读写权限,确保当前用户可访问;必要时以管理员身份运行脚本
3. 排查端口冲突
--remote-debugging-port=9222可能被其他进程占用,导致Chrome启动后直接退出。可以更换端口(比如9223),或暂时移除该参数测试:
# chrome_options.add_argument("--remote-debugging-port=9222") # 注释该行或修改为其他端口
4. 保证ChromeDriver与Chrome版本匹配
ChromeDriver版本必须和本地安装的Chrome主版本完全一致(例如Chrome是120.x,ChromeDriver也要用120.x版本)。可在Chrome的设置->关于Chrome查看版本,下载对应版本的ChromeDriver替换现有文件。
5. 简化参数排查冲突
暂时移除非必要参数,仅保留核心配置验证是否能启动:
from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_profile_path = r'C:\Users\tan.le\AppData\Local\Google\Chrome\User Data' profile_name = "Profile 2" chrome_options = Options() chrome_options.add_argument(f'--user-data-dir={chrome_profile_path}') chrome_options.add_argument(f"--profile-directory={profile_name}") driver = webdriver.Chrome(options=chrome_options) driver.get('https://www.google.com/search?q=ch%C3%BA+voi+connnn')
若正常启动,再逐个添加之前的参数,定位导致冲突的选项。
修复后的完整代码示例
整合上述修复点的代码:
from selenium import webdriver from selenium.webdriver.chrome.options import Options # 原始字符串避免路径转义问题 chrome_profile_path = r'C:\Users\tan.le\AppData\Local\Google\Chrome\User Data' profile_name = "Profile 2" chrome_options = Options() # 更换未被占用的调试端口 chrome_options.add_argument("--remote-debugging-port=9223") chrome_options.add_argument("--disable-dev-shm-usage") chrome_options.add_argument(f'--user-data-dir={chrome_profile_path}') chrome_options.add_argument(f"--profile-directory={profile_name}") chrome_options.add_argument("start-maximized") chrome_options.add_argument("disable-infobars") chrome_options.add_argument("--disable-extensions") chrome_options.add_argument("--disable-gpu") chrome_options.add_argument("--no-sandbox") chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"]) chrome_options.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=chrome_options) url = 'https://www.google.com/search?q=ch%C3%BA+voi+connnn' driver.get(url)
内容的提问来源于stack exchange,提问作者Le Trong Tan
相关产品推荐
相关产品推荐

