ChromeDriver に PATHを通していない場合のエラーの対処方法

スポンサーリンク

Seleniumを使用し、PythonでGoogleChromeを起動し、Webページを表示する場合にChromeDriverが必要となりますが、ChromeDriverにPATHを通していない場合はエラーが発生します。

このエラーの対処方法について解説します。

なお、Seleniumモジュールは、Pythonの標準ライブラリではありませんので、事前にインストールする必要はありません。

■Python

今回のPythonのバージョンは、「3.8.5」を使用しています。(Windows10)(pythonランチャーでの確認)

■発生したエラー

Traceback (most recent call last):
File “C:\pg\Python38\lib\site-packages\selenium\webdriver\common\service.py”, line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File “C:\pg\Python38\lib\subprocess.py”, line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File “C:\pg\Python38\lib\subprocess.py”, line 1307, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] 指定されたファイルが見つかりません。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\user\test0107.py”, line 7, in <module>
driver = webdriver.Chrome()
File “C:\pg\Python38\lib\site-packages\selenium\webdriver\chrome\webdriver.py”, line 73, in __init__
self.service.start()
File “C:\pg\Python38\lib\site-packages\selenium\webdriver\common\service.py”, line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

「selenium.common.exceptions.WebDriverException: Message」のメッセージ内容を確認すると、「実行ファイル’chromedriver’はPATHになければなりません。https://sites.google.com/a/chromium.org/chromedriver/homeを参照してください。(’chromedriver’ executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home)」と書かれています。

■対処方法

driver = webdriver.Chrome()

今回実行したスクリプトのコードには、webdriver.Chrome()の括弧内に、実行されるファイルである「chromedriver」の指定されていません。

指定されていませんので、今回のようなエラーが発生します。これを対処するために、ChromeDriverをダウンロードし解凍した後のフォルダ内にある「chromedriver.exe」のファイルを指定します。

driver = webdriver.Chrome(executable_path=r'C:\Users\user\.wdm\drivers\chromedriver\win32.0.4280.88\chromedriver.exe')

指定する場合は、webdriver.Chrome()の括弧内に、executable_pathと記述し、「chromedriver.exe」を指定します。

PythonでGoogleChromeを自動的に起動し、Webページを表示させることができるようになります。

コメント

タイトルとURLをコピーしました