【Python】SeleniumでDeprecationWarning: executable_pathの対処方法

2023.03.05 /

【Python】SeleniumでDeprecationWarning: executable_pathの対処方法

記事ではPythonSeleniumにおける、DeprecationWarning: executable_path has been deprecatedの対処方法について解説していきます。

Seleniumをバージョン4にバージョンアップした後に、今までに作成したSeleniumを使用したプルゴラムを実行するとDeprecationWarning: executable_path has been deprecatedの警告が表示されることがあります。

このDeprecationWarningの意味や対処方法について、本記事を通して理解を深めてください。

PythonでSeleniumによるブラウザの操作・制御を行う方法については以下記事をご参照ください。

Seleniumで表示されるようになった警告

Seleniumのバージョン4は2021年10月にリリースされました。Python環境でSeleniumをバージョンアップした後に、Seleniumのバージョン3で作成したプログラムを実行すると次の警告文が表示されるようになります。

表示される警告文

DeprecationWarning: executable_path has been deprecated, please pass in a Service object
DeprecationWarning: use options instead of chrome_options

この警告文が表示されたプログラムの一部を以下に記します。

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--disable-gpu');
options.add_argument('--disable-extensions');
options.add_argument('--start-maximized');
options.add_experimental_option("excludeSwitches", ['enable-automation'])

chromedriver = 'C:\Office54\chromedriver.exe'
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=options)
driver.get("https://office54.net/")

警告文が出ないように、このコードをどのように改善すればよいのか解説していきます。

Selenium:DeprecationWarningの意味

Deprecationとは日本語で非推奨という意味です。DeprecationWarningとは非推奨の警告、つまりDeprecationWarningは非推奨の機能を使用した場合に表示される警告文ということです。

非推奨ということで、プログラム自体は問題なく動作します。ただSeleniumがバージョンアップすることで非推奨の機能が廃止される可能性は高いです。

今回で言うとSeleniumバージョン4ではDeprecationWarningのメッセージに記載があるようにexecutable_pathとchrome_optionsが非推奨となりました。

現在ではまだプログラムを問題なく使用できますが、今後Seleniumがバージョンアップすると廃止され、プログラムが使用できなくなる恐れがあります。そのため早い段階でSeleniumバージョン4で推奨している方法を使うようにしましょう。

Selenium:DeprecationWarningの対処方法

Seleniumのバージョン4からは、Chrome()メソッドの引数にexecutable_pathを指定せずに、Serviceオブジェクトにexecutable_pathを指定するようにします。

Serviceを利用するためにはプログラムの先頭でServiceをインポートします。

from selenium.webdriver.chrome.service import Service

次にServiceを介して、executable_pathを指定します。

chromedriver = 'C:\Office54\chromedriver.exe'
service = Service(executable_path=chromedriver)

Chrome()メソッドには引数にserviceを指定して渡します。

driver = webdriver.Chrome(service=service)

次にDeprecationWarning: use options instead of chrome_optionsに対処するため、Chrome()メソッドではchrome_optionsではなく、optionsを使用します。

driver = webdriver.Chrome(service=service, options=options)

上記を踏まえ、最終的にコードを以下のように書き直すことで警告文が表示されなくなります。

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

options = webdriver.ChromeOptions()
options.add_argument('--disable-gpu');
options.add_argument('--disable-extensions');
options.add_argument('--start-maximized');
options.add_experimental_option("excludeSwitches", ['enable-automation'])

chromedriver = 'C:\Office54\chromedriver.exe'
service = Service(executable_path=chromedriver)
driver = webdriver.Chrome(service=service, options=options)
driver.get("https://office54.net/")

まとめ

本記事「【Python】SeleniumでDeprecationWarning: executable_pathの対処方法」はいかがでしたか。

ぜひ最新のSeleniumに対応するように、プログラムを修正してください。

Seleniumバージョン4では他にも廃止されているメソッドがあります。プログラムが使用できなくなるので、できるだけ早く対応するようにしてください。