首页 > 解决方案 > DeprecationWarning: executable_path has been deprecated, please pass in a Service object

问题描述

我今天开始了一个 selenium 教程,并在尝试运行代码时遇到了这个错误。我尝试了其他方法,但最终得到了同样的错误。我在使用 VSC 的 MacOS 上。

我的代码:

from selenium import webdriver

PATH = '/Users/blutch/Documents/Chrom Web Driver\chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get("https://www.google.com")

我也试过在 /Users 前面插入 C:。谁能指导我为什么会发生这种情况/如何解决它?

标签: python-3.xseleniumselenium-webdriverselenium-chromedriver

解决方案


此错误消息...

DeprecationWarning: executable_path has been deprecated, please pass in a Service object

...意味着该密钥 executable_path将在即将发布的版本中被弃用。

此更改与Selenium 4.0 Beta 1 更改日志一致,其中提到:

弃用驱动程序实例化中除Options和参数之外的所有参数。Service(#9125,#9128)


解决方案

弃用密钥 后,executable_path您必须使用Service()该类的实例,如下所示:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

s = Service('C:/Users/.../chromedriver.exe')
driver = webdriver.Chrome(service=s)

TL; 博士

您可以在以下位置找到一些相关的详细讨论:


推荐阅读