You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac下用Python Selenium调用Chrome的路径配置及报错解决方案

报错原因

这个DeprecationWarning是Selenium 4.0及以上版本的语法变更导致的:旧版直接将chromedriver路径作为executable_path参数传入webdriver.Chrome()的写法已被官方弃用,要求通过Service对象传递驱动路径配置。

Mac系统下的修复步骤

  • 第一步:从selenium模块导入Service类
  • 第二步:用你的chromedriver路径初始化Service对象
  • 第三步:将Service对象作为service参数传入webdriver.Chrome()构造函数

修改后的完整代码

import time
from selenium import webdriver
# 导入Service类
from selenium.webdriver.chrome.service import Service

# 替换为你的chromedriver实际路径初始化Service
s = Service('../chromedriver')
driver = webdriver.Chrome(service=s)

driver.get('http://www.google.com/')
time.sleep(5) 
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5)
driver.quit()

补充优化方案

如果不想每次写代码都指定chromedriver路径,可以把下载好的对应版本chromedriver移动到Mac的系统PATH目录下,执行终端命令:
sudo mv 你的chromedriver本地路径 /usr/local/bin/
之后初始化Service时不需要传路径,直接写Service()即可自动识别。

如果你的Chrome浏览器安装在非默认路径,还可以通过ChromeOptions指定浏览器本体路径:

from selenium.webdriver.chrome.options import Options

chrome_options = Options()
# 替换为你的Chrome.app实际路径
chrome_options.binary_location = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
driver = webdriver.Chrome(service=s, options=chrome_options)

内容的提问来源于stack exchange,提问作者Pavel Danilov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 00:24:03