Selenium加载自定义Chrome配置文件失败:无法连接服务问题
问题分析:Chrome自定义配置文件启动失败无法连接服务
尝试使用自定义Google配置文件访问指定URL,Chrome配置文件窗口弹出后无任何操作,一段时间后抛出无法连接服务的错误,错误栈如下:
Traceback (most recent call last): File "c:\Users\prodi\Desktop\auto job applying bot\auto-job-applying-bot\Test Code\main.py", line 52, in <module> driver = webdriver.Chrome(service=service,options=chrome_options) File "C:\Users\prodi\Desktop\auto job applying bot\auto-job-applying-bot\env\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 80, in __init__ super().__init__( File "C:\Users\prodi\Desktop\auto job applying bot\auto-job-applying-bot\env\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 101, in __init__ self.service.start() File "C:\Users\prodi\Desktop\auto job applying bot\auto-job-applying-bot\env\lib\site-packages\selenium\webdriver\common\service.py", line 113, in start raise WebDriverException(f"Can not connect to the Service {self.path}") selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
已尝试的无效方案:
- 确认文件路径正确
- 参考方案使用Cookie
问题根源
你犯了核心错误:Selenium的Service参数需要指定的是ChromeDriver的路径,而非Chrome浏览器程序(chrome.exe)的路径。错误信息里明确显示服务路径指向chrome.exe,但ChromeDriver才是Selenium驱动Chrome的桥梁程序,两者不能混淆。
解决步骤
下载匹配版本的ChromeDriver
- 打开Chrome,在地址栏输入
chrome://version/查看你的Chrome版本号 - 下载与Chrome版本完全一致的ChromeDriver(注意区分系统位数)
- 打开Chrome,在地址栏输入
修正Service配置
在代码里把Service的路径改成ChromeDriver.exe的实际位置,示例:from selenium.webdriver.chrome.service import Service # 替换为你的ChromeDriver路径 service = Service(executable_path=r"C:\xxx\chromedriver.exe") driver = webdriver.Chrome(service=service, options=chrome_options)检查自定义配置文件参数
确保用户数据目录的配置格式正确,示例:chrome_options.add_argument(r"--user-data-dir=C:\Users\你的用户名\AppData\Local\Google\Chrome\User Data") chrome_options.add_argument(r"--profile-directory=Profile 1") # 替换为你的配置文件夹名注意:运行脚本前关闭所有Chrome窗口,避免同一用户数据目录被多进程占用引发冲突。
额外排查点
- 确认ChromeDriver有执行权限,可尝试右键以管理员身份运行脚本
- 可将ChromeDriver加入系统环境变量
PATH,这样代码中无需写完整路径,直接service = Service()即可
内容的提问来源于stack exchange,提问作者user21478867
相关产品推荐
相关产品推荐

