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

Posted in :

在切換到新的版本的 selenium 時,顯示的錯誤訊息。

解法如下:
https://stackoverflow.com/questions/64717302/deprecationwarning-executable-path-has-been-deprecated-selenium-python

或:
https://stackoverflow.com/questions/69918148/deprecationwarning-executable-path-has-been-deprecated-please-pass-in-a-servic/69918489

before:

from selenium import webdriver
chrome_driver_path = 'C:/Users/Morteza/Documents/Dev/chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver_path)

url = "https://www.google.com"
driver.get(url)

after:

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

s=Service('C:/Users/Morteza/Documents/Dev/chromedriver.exe')
browser = webdriver.Chrome(service=s)
url='https://www.google.com'
browser.get(url)

This error message…

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

…implies that the key executable_path will be deprecated in the upcoming releases.

This change is inline with the Selenium 4.0 Beta 1 changelog which mentions:

Deprecate all but Options and Service arguments in driver instantiation. (#9125,#9128)


Solution

Once the key executable_path is deprecated you have to use an instance of the Service() class as follows:

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

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

TL; DR

You can find a couple of relevant detailed discussion in:

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *