2016-11-05 82 views
1

我想學習python網頁抓取,但我無法讓硒與任一瀏覽器一起工作。硒不會與火狐或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' 

我試圖從添加路徑代碼

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

,以增加它的路徑在我的環境變量的一切,我可以在互聯網上找到。我似乎無法弄清楚...

+0

貌似http://stackoverflow.com/的重複問題/ 40208051/selenium-using-python-geckodriver-executable-needs-in-path –

回答

4

對於Firefox和Chrome,您現在需要下載geckodriver/chromedriver。這些驅動程序是您安裝的瀏覽器與硒之間進行通信所必需的。因此,你需要:

  • 爲Python安裝硒爲您要使用的瀏覽器(chromedriver,geckodriver,operadriver等)
  • 安裝你想你的系統上使用的瀏覽器(pip install selenium
  • 下載drivers (可能已經有這個)

現在,您可以將geckodriver添加到您的路徑中,如anwser中所述。

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

Firefox的:或者你可以在你的代碼很喜歡這種直接設置它 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。你需要從enter link description here下載它,把EXE在Python腳本所在的目錄,並嘗試下面的代碼:

請試試這個代碼:

# -*- 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()