错误描述: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
触发原因
DeprecationWarning 类型的警告错误:大多是属于版本已经更新,所使用的方法已经过时。
之所以出现这样的错误 是因为executable_path 被重构到了 Service 函数里,WebDriver类中加入了判断,
if executable_path != 'chromedriver':
warnings.warn('executable_path has been deprecated, please pass in a Service object',
原报错代码
# -*- coding: utf-8 -*-
from selenium import webdriver
def getChromeBrowser():
# 指定浏览器内核位置
chrome_driver = webdriver.Chrome(r'E:\chromedriver_win32\chromedriver.exe')
return chrome_driver
修改后的代码
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
def getChromeBrowser():
# 指定浏览器内核位置
c_service = Service(r'E:\chromedriver_win32\chromedriver.exe')
chrome_driver = webdriver.Chrome(service=c_service)
return chrome_driver
这样既可解决DeprecationWarning: executable_path has been deprecated, please pass in a Service object *…的报错,再次运行,就不会再触发DeprecationWarning: executable_path has been deprecated, please pass in a Service object *…报错啦。