2016-11-05 3 views
1

파이썬 웹 스크래핑을 배우려고하지만 셀렌이 어느 브라우저에서든 작동하지 않습니다.셀레늄이 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' 

내 환경 변수에서 그것의 경로를 추가하는 코드

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

의 경로를 추가하는 내가 인터넷에서 찾을 수있는 모든 것을 시도했다. 나는 이것을 알아낼 수 없습니다 ...

+0

이 http://stackoverflow.com/의 중복 것 같은데 :

이 코드를 시도하십시오 : 아래 코드 enter link description here에서 다운로드 파이썬 스크립트가있는 디렉토리에 exe를 넣어 시도해야 질문/40208051/selenium-using-python-geckodriver-executable-need-to-be-in-path –

답변

4

Firefox와 Chrome 모두 이제 geckodriver/chromedriver을 다운로드해야합니다.이 드라이버는 설치된 브라우저와 셀렌 사이의 통신에 필요합니다. 그래서 당신이 필요합니다

  • 파이썬을 위해 셀레늄을 설치 사용할 브라우저 (chromedriver, geckodriver, operadriver 등)
  • 시스템에 사용할 브라우저를 설치하기위한 (pip install selenium)
  • 다운로드 drivers을 (아마도 이미 이것을 가지고있을 것입니다)

이제 anwser에 나와있는대로 geckodriver를 경로에 추가 할 수 있습니다.

쵸메 : driver = webdriver.Chrome(executable_path='/path/to/chromedriver.exe')

파이어 폭스 : 당신의 메시지 라인에 따르면 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() 
관련 문제