解决 DeprecationWarning: Executable executable_path has been deprecated, please pass in a Service obje

1、错误脚本:

# 导入selenium
import time

from selenium import webdriver

# 选择谷歌浏览器
driver = webdriver.Chrome(executable_path=r'C:\Program Files\python39\chromedriver.exe')
# 输入网址
driver.get("https://www.baidu.com/")
# 操作网址
time.sleep(3)
# 打印网页title
print(driver.title)
# 关闭网址
driver.quit()

错误结果:

解决 DeprecationWarning: Executable executable_path has been deprecated, please pass in a Service obje

2、错误原因

出现  DeprecationWarning  警告的类型错误:

该类型的警告大多属于版本更新时,所使用的方法过时的原因;某方法在当前版本被重构,依旧可以传入参数,但是在之后的某个版本会被删除。

3、解决方案

# 导入selenium
import time

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

# 选择谷歌浏览器
s = Service(executable_path=r'C:\Program Files\python39\chromedriver.exe')
driver = webdriver.Chrome(service=s)
# 输入网址
driver.get("https://www.baidu.com/")
# 操作网址
time.sleep(3)
# 打印网页title
print(driver.title)
# 关闭网址
driver.quit()

正确结果:

解决 DeprecationWarning: Executable executable_path has been deprecated, please pass in a Service obje

 

上一篇:PySide6技巧--多线程和信号发送函数


下一篇:动态HTML处理(Selenium爬取斗鱼详情)