2016-11-05 5 views
1

私はPythonのWebスクレイピングを学ぼうとしていますが、どちらのブラウザでも動作するようにセレンを得ることはできません。セレンはFirefoxまたはChromeで動作しません

from selenium import webdriver 
browser = webdriver.Firefox() 

これは私が持っているすべてのコードであり、私はこれをエラーとして受け取ります。

Traceback (most recent call last): 
    File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 64, in start 
    stdout=self.log_file, stderr=self.log_file) 
    File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 950, in __init__ 
    restore_signals, start_new_session) 
    File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 1220, in _execute_child 
    startupinfo) 
FileNotFoundError: [WinError 2] The system cannot find the file specified 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "H:\codingpractice\python\python challenge.com.py", line 2, in <module> 
    browser = webdriver.Firefox() 
    File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135, in __init__ 
    self.service.start() 
    File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\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. 

Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00A11350>> 
Traceback (most recent call last): 
    File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__ 
    self.stop() 
    File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop 
    if self.process is None: 
AttributeError: 'Service' object has no attribute 'process' 

私は私の環境変数でPATHを追加するコード

from selenium import webdriver 
browser = webdriver.Firefox("C:\Program Files (x86)\Mozilla Firefox\firefox.exe") 

へのパスを追加することから、私はインターネット上で見つけることができるすべてのものを試してみました。私はこれを把握することができません...

+0

がhttp://stackoverflow.com/の重複のように見える:

は、このコードを試してください:あなたは以下のコード、enter link description hereからそれをダウンロードするPythonスクリプトが置かれているディレクトリにexeファイルを入れてみてくださいする必要があります質問/ 40208051 /セレンを使用した - python-geckodriver実行可能ファイル - パスにする必要があります –

答えて

4

あなたは今すぐgeckodriver/chromedriverをダウンロードする必要があります。これらのドライバは、インストールされているブラウザとセレンの間の通信に必要です。だから、必要があります。

  • のpythonのためのセレンをインストールし、使用するブラウザ(chromedriver、geckodriver、operadriverなど)
  • あなたのシステム上で使用するブラウザをインストール用(pip install selenium
  • ダウンロードdrivers

ここで、anwserに記載されているように、パスにgeckodriverを追加することができます。それともあなたはこれを好きなコード内で直接それを設定することができます

丁目: driver = webdriver.Chrome(executable_path='/path/to/chromedriver.exe')

のFirefox:あなたのメッセージの1行によると driver = webdriver.Firefox(executable_path='/opt/geckoDriver/geckodriver.exe')

+0

私はあなたの情報を扱うことができました。ありがとうございました。 – AutomateMyJob

0

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

そうでありませんgeckodriver.exeを持っています。

# -*- coding: utf-8 -*- 

from selenium import webdriver 
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary 

gecko = os.path.normpath(os.path.join(os.path.dirname(__file__), 'geckodriver')) 
binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe') 
browser = webdriver.Firefox(firefox_binary=binary,executable_path=gecko+'.exe') 
browser.get('http:///www.google.com') 
browser.close() 
関連する問題