Selenium导入谷歌浏览器配置时executable_path弃用警告问题求助
解决Selenium中executable_path弃用的问题
错误原因
你碰到的DeprecationWarning是因为Selenium 4.x版本起,不再支持通过executable_path参数指定ChromeDriver路径,必须改用Service对象来完成这个配置。
正确修改步骤
- 导入Service类:从selenium的Chrome服务模块里导入Service类
- 创建Service实例:把ChromeDriver的完整路径传入Service的构造函数
- 初始化Chrome驱动:将Service对象传给
webdriver.Chrome的service参数,同时保留原有的options配置
修正后的完整代码
from selenium import webdriver from selenium.webdriver.chrome.service import Service # 新增导入Service类 from selenium.webdriver.common.keys import Keys options = webdriver.ChromeOptions() # 用原始字符串写法避免转义麻烦,可读性更强 options.add_argument(r"user-data-dir=C:\Users\CharlieWait\AppData\Local\Google\Chrome\User Data") # 实例化Service对象,传入chromedriver的路径 service = Service(r"C:\Users\CharlieWait\chromedriver.exe") # 把service和options参数传给Chrome驱动 w = webdriver.Chrome(service=service, options=options)
排查额外问题(如果仍报错)
- 确认ChromeDriver版本和你的Chrome浏览器版本完全匹配,版本不兼容会直接导致驱动启动失败
- 检查ChromeDriver的路径是否正确,路径里如果有空格或特殊字符,确保用引号包裹
- 关闭所有Chrome浏览器窗口后再运行代码,避免进程占用冲突
内容的提问来源于stack exchange,提问作者Charlie Wait
相关产品推荐
相关产品推荐

