DepreventionWarning:executable pathは破棄されました


ケース


「Chromedriverパス」では、ここです。から最新のChromeドライババージョンをダウンロードし、次のようにChrome Webドライバを実行します.
from selenium import webdriver


def set_chrome_driver():
    chrome_options = webdriver.ChromeOptions()
    driver = webdriver.Chrome("chromedriver경로", options=chrome_options)
    return driver
しかし、ある瞬間からpytestを実行すると以下の警告が表示されます.DeprecationWarning: executable_path has been deprecated, please pass in a Service object

解決策

  • webdriver-managerパッケージをインストールします.
  • $ pip install webdriver-manager
    次のように、WebDriver-ManagerのChromeDriver Managerを使用して、次のように現在のOSにインストールされているChromeブラウザを使用して、サービス・オブジェクト上でChromeブラウザを変更します.
    (「サービス」キーワード引数はSelenium 4から使用されるようです.)
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from webdriver_manager.chrome import ChromeDriverManager
    
    
    def set_chrome_driver():
        chrome_options = webdriver.ChromeOptions()
        driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
        return driver

    やり直し結果


    pytestを再実行すると、テスト結果が警告なしにきれいに保たれることを確認できます.

    以前は、ダウンロードしたクロムドライバが現在使用しているクロムブラウザバージョンよりも低いため、SessionNotCreatedExceptionによく遭遇していましたが、今はクロムドライバを手動でダウンロードする必要がなくなり、快適でした:)

    Reference

  • https://stackoverflow.com/questions/64717302/deprecationwarning-executable-path-has-been-deprecated-selenium-python