seleniumでChromeDriverを使用したいのですがエラーが出てしまいます。
対処法がわかりません。
エラーの原因と対処方法がわかる方がいればアドバイスお願いいたします。
D:\DEV\AAA\main.py
1from selenium import webdriver 2 3driver = webdriver.Chrome(executable_path="D:\DEV\AAAr\chromedriver.exe")
上記コードを実行すると以下のエラーがでます。
エラーメッセージ PS C:\Users\UserName> & C:/Users/UserName/AppData/Local/Programs/Python/Python39/python.exe d:/DEV/AAA/main.py d:\DEV\AAA\main.py:3: DeprecationWarning: executable_path has been deprecated, please pass in a Service object driver = webdriver.Chrome(executable_path="D:\DEV\AAA\chromedriver.exe") Traceback (most recent call last): File "C:\Users\UserName\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start self.process = subprocess.Popen(cmd, env=self.env, File "C:\Users\UserName\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Users\UserName\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, 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 "d:\DEV\AAA\main.py", line 3, in <module> driver = webdriver.Chrome(executable_path="D:\DEV\AAA\chromedriver.exe") File "C:\Users\UserName\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 70, in __init__ super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog", File "C:\Users\UserName\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 90, in __init__ self.service.start() File "C:\Users\UserName\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start raise WebDriverException( selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home
ChromeDriverの環境変数Pathが通っていないことが原因だと考え、
こちらの記事を参考に環境変数を設定しました。
1.「コントロールパネル」を開く 2.「システムとセキュリティ」を開く 3.「システム」を開く 4.「システムの詳細設定」を開く 5.「環境変数」を開く 6.「システム環境変数」から「Path」を選択して「編集」をクリック 7.「新規」をクリックしてパスを追加 8.各ページを「OK」で閉じて再起動
今回の環境だと、D:\DEV\AAA\にchromedriver.exeがあるので、7.「新規」をクリックしてパスを追加の工程でD:\DEV\AAAを追加しました。
もう一度実行してみましたが、同じエラーがでます。
7.「新規」をクリックしてパスを追加の工程でD:\DEV\AAA\を追加してみたりしましたが、結果は変わりません。
デスクトップ上に作成したシステム環境変数を設定していないchromedriver.exeを参照すると普通に動きます。
このことから、Dドライブにおいた.pyやchromedriver.exeを使用するには適切なディレクトリにcdするような処理が必要になるのかなと考えているのですがなにをすればいいかわかりません。
回答1件
あなたの回答
tips
プレビュー