Chrome Driver报错求助:AttributeError: 'Service'对象无'process'属性
解决Chrome Driver的DeprecationWarning和AttributeError问题
Selenium 4.x版本开始已废弃直接在webdriver.Chrome()中传入executable_path的写法,必须通过Service对象管理驱动路径。你遇到的AttributeError大概率是未正确导入Service类或写法有误,以下是修正后的代码:
from selenium import webdriver import time from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.service import Service # 必须导入Service类 # 初始化Service对象,指定ChromeDriver路径 service = Service(r"/usr/local/bin/chromedriver") # 将service参数传入webdriver.Chrome() driver = webdriver.Chrome(service=service)
额外排查建议:
- 确认Selenium版本为4.x以上,执行
pip show selenium查看版本,低于4.x则运行pip install --upgrade selenium升级 - 再次核对Chrome浏览器和ChromeDriver的大版本号完全一致(比如Chrome是118.x,Driver也要对应118.x)
- 不想手动管理驱动路径的话,可使用
webdriver-manager自动匹配下载:
先安装依赖:pip install webdriver-manager
代码简化为:from selenium import webdriver from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
内容的提问来源于stack exchange,提问作者Whywhywhy
相关产品推荐
相关产品推荐

