解決 DeprecationWarning: Executable executable_path has been deprecated, please pass in a Service object in Selenium Python 問題


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()

錯誤結果:

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()

正確結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM