私はSweigartの「退屈な作業をPythonで自動化する」というテキストを読んでいます。アイドルSelenium モジュールと Firefox ブラウザがすでにインストールされています。
Webdriver 関数を実行しようとすると、次のメッセージが表示されます。
from selenium import webdriver
browser = webdriver.Firefox()
例外:
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0DA1080>>
Traceback (most recent call last):
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
self.stop()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0E08128>>
Traceback (most recent call last):
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
self.stop()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Traceback (most recent call last):
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\Python\Python35\lib\subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "C:\Python\Python35\lib\subprocess.py", line 1224, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
上記の例外の処理中に、別の例外が発生しました:
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
browser = webdriver.Firefox()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135, in __init__
self.service.start()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
のパスを設定する必要があると思いますgeckodriver
が、方法がわかりません。どうすればいいでしょうか?
ベストアンサー1
selenium.common.exceptions.WebDriverException: メッセージ: 'geckodriver' 実行可能ファイルが PATH に存在する必要があります。
まず、Seleniumを使用して最新のFirefoxを実行するには、ここから最新の実行可能geckodriverをダウンロードする必要があります。
実際には、Selenium クライアント バインディングはgeckodriver
システムから実行可能ファイルを見つけようとしますPATH
。実行可能ファイルを含むディレクトリをシステム パスに追加する必要があります。
Unix システムでは、Bash 互換シェルを使用している場合は、次のようにしてシステムの検索パスに追加できます。
export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step
Windowsでは、実行可能ファイルgeckodriverへの完全なディレクトリパスを追加するためにPathシステム変数を更新する必要があります。 手動でまたはコマンドライン** (実行可能 geckodriver をシステム PATH に追加した後、システムを再起動して有効にすることを忘れないでください)**。原理は Unix の場合と同じです。
これで、以下のようにコードを実行できます:-
from selenium import webdriver
browser = webdriver.Firefox()
selenium.common.exceptions.WebDriverException: メッセージ: ブラウザのバイナリの場所が予想されましたが、デフォルトの場所にバイナリが見つかりません。'moz:firefoxOptions.binary' 機能が提供されておらず、コマンド ラインにバイナリ フラグが設定されていません。
この例外は、Selenium が Firefox を見つけてデフォルトの場所から起動しようとしているが、Firefox が別の場所にインストールされていることを明確に示しています。Firefox を起動するには、以下のように Firefox がインストールされているバイナリの場所を明示的に指定する必要があります。
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('path/to/installed firefox binary')
browser = webdriver.Firefox(firefox_binary=binary)