文章内容

2022/3/12 16:32:52,作 者: 黄兵

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

在使用 Python Selenium 的时候,使用 Chrome 浏览器进行获取内容,出现如下警告:

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

出现问题的原因:

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

具体错误代码如下:

driver = webdriver.Chrome(executable_path=os.getenv('CHROME_PATH'))
driver.get(link)

解决方案:

Selenium 4 由于不推荐使用 executable_path,因此您必须使用 Service() 该类的实例以及 ChromeDriverManager().install() 下面讨论的命令。

首先将 Selenium 升级到 v4.0.0 以上:

pip3 install -U selenium

安装 Python 的 Webdriver Manager:

pip3 install webdriver_manager

Selenium v​​4 兼容代码块

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

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.pdflibr.com")

参考资料:

1、DeprecationWarning: executable_path has been deprecated selenium python

黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - DeprecationWarning: executable_path has been deprecated, please pass in a Service object

分享到:

发表评论

评论列表