2016-10-15 96 views
4

我最近下載的硒,並試圖運行此腳本:蟒蛇的webdriver eror

from selenium import webdriver 
driver = webdriver.Firefox() 

,但我得到這個錯誤:

Traceback (most recent call last): 
    File "<pyshell#3>", line 1, in <module> 
    driver = webdriver.Firefox() 
    File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\site-packages\selenium-3.0.1-py2.7.egg\selenium\webdriver\firefox\webdriver.py", line 145, in __init__ 
    keep_alive=True) 
    File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\site-packages\selenium-3.0.1-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 92, in __init__ 
    self.start_session(desired_capabilities, browser_profile) 
    File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\site-packages\selenium-3.0.1-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 179, in start_session 
    response = self.execute(Command.NEW_SESSION, capabilities) 
    File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\site-packages\selenium-3.0.1-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 236, in execute 
    self.error_handler.check_response(response) 
    File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\site-packages\selenium-3.0.1-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 192, in check_response 
    raise exception_class(message, screen, stacktrace) 
WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'firefox_binary' capability provided, and no binary flag set on the command line 

哦,順便說一下這個打開我面前geckodriver.exe其寫錯誤

+0

並且您是否安裝了Firefox?你有沒有試過告訴Selenium它在哪?如果你使用的是便攜式應用,他們可能會把事情放在非傳統的地方,這會破壞東西。 – jonrsharpe

+0

感謝評論,我安裝了fire fox,我怎麼告訴硒在哪裏?我使用便攜式應用程序,你認爲我應該刪除它,並沒有它下載python? – omersk

+0

你正在使用哪個版本的Firefox? – Piyush

回答

7

我通過手動設置二進制位置解決了這個問題,如下面的答案:

https://stackoverflow.com/a/25715497

記得設置你的二進制文件到firefox二進制文件的實際位置在您的計算機上

如:

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary 

binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe') 
self.browser = webdriver.Firefox(firefox_binary=binary) 

(注:在文件路徑中的第一次報價之前的「R」字符串會使python看到字符串爲'raw'文本,因此您不需要像'\'那樣轉義字符 - 您可以將其放在路徑中,因爲它實際上是)