0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SeleniumのDeprecationWarningを解消:WebDriverの新しい利用法

0
Posted at

この記事は、個人技術ブログ CodeArchPedia.com の技術メモ(要約)です。

Seleniumを使っていると、急にDeprecationWarning: executable_pathという警告が出てきて、少し面倒に感じた話です。古いコードベースをメンテナンスしていて、この警告が出たままデプロイするのは避けたい。この警告は、Selenium 4以降でのWebDriver初期化の作法が変わったことを示しています。

何が起きたか(課題)

WebDriverの初期化方法が古くなっているために、以下の警告がログに出力され続けます。

  • DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  • この警告は、将来的に引数が削除されることを示唆しており、コードの安定性を損なう原因になる。
  • 手動でchromedriverなどのパスを指定する方法は、環境が変わるとすぐに壊れるリスクがある。

どう解決したか(概要)

解決策は、大きく分けて2つのステップ、あるいは最新の方法を適用することです。

  1. Serviceオブジェクトへの移行: 以前のexecutable_path引数の代わりに、Serviceオブジェクトを生成し、その中にドライバの実行パスや設定をまとめる方法に切り替えました。これにより、設定が一箇所に集約され、コードの可読性が上がります。

  2. Selenium Managerの活用 (推奨): Selenium 4.6.0以降であれば、ほとんどの場合、Serviceオブジェクトすら不要になります。webdriver.Chrome()のように引数なしで呼び出すだけで、Selenium Managerが裏側で適切なWebDriverを自動的にダウンロード・パス解決してくれます。これが現在の最もシンプルで堅牢なアプローチです。

効果(Before/After)

この移行により、運用上の明確なメリットが得られました。

  • Before: executable_path指定により、OSやライブラリバージョンが変わるたびに手動でパス修正が必要になることがあった。
  • After (Selenium Manager利用時): 環境依存性が大幅に減少し、テスト実行環境のセットアップにかかる時間が劇的に短縮された。コードからパス指定が消えるため、メンテナンス性が向上した。

Serviceオブジェクトを利用することで、WebDriverプロセス管理が明確になり、driver.quit()時の挙動も安定しました。


🚀 詳細な設定とコードはこちら

具体的なWAFのルール設定や、より詳細なログ解析データは元のブログで公開しています。

👉 SeleniumのDeprecationWarningを解消:WebDriverの新しい利用法

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?