2016-11-20 43 views
2

我想使用硒處理一些使用JavaScript代碼的網站。首先,我使用下面的簡單例子如何解決Python Selenium錯誤

from selenium import webdriver 

browser = webdriver.Firefox() 

browser.get('http://www.yahoo.com') 

browser.quit() 

雖然這甚至不起作用。錯誤消息如下所示。我想我可能會錯過硒或其他東西。你能爲我帶來一些啓示嗎?

Traceback (most recent call last): 
    File "/home/rnie/webdriver-tutorial-1x.py", line 3, in <module> 
    browser = webdriver.Firefox() 
    File "/usr/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 145, in __init__ 
    keep_alive=True) 
    File "/usr/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__ 
    self.start_session(desired_capabilities, browser_profile) 
    File "/usr/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session 
    response = self.execute(Command.NEW_SESSION, capabilities) 
    File "/usr/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute 
    self.error_handler.check_response(response) 
    File "/usr/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response 
    raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.WebDriverException: Message: connection refused 

我檢查了"geckodriver.log",它顯示了以下錯誤消息:

1479623778556 geckodriver INFO Listening on 127.0.0.1:49723 
1479623778625 mozprofile::profile INFO Using profile path /tmp/rust_mozprofile.WMDGNONHQTud 
1479623778628 geckodriver::marionette INFO Starting browser /usr/bin/firefox 
1479623778641 geckodriver::marionette INFO Connecting to Marionette on localhost:35691 
No protocol specified 
Unable to init server: Could not connect: Connection refused 
Error: cannot open display: :0 

回答

-1

這些天Firefox是不是做與硒的網絡驅動器多好,我可以建議你使用Chrome驅動程序,而

下載chrome驅動程序並提取文件。設置鉻驅動路徑中的環境的路徑,然後通過替換火狐()到鉻() 用相同的代碼嘗試或按照下列代碼....

from selenium import webdriver 

Path = ("c://chromedriver//chromedriver.exe") 
browser = webdriver.Chrome(path) 
#eliminate above two steps if the path is set in Environment variable 

browser = webdriver.Chrome() 

browser.get('http://www.yahoo.com') 

browser.quit() 
+1

是的,我改變爲鉻,我的問題在這裏解決。 – Robin

0

硒3.0釋放,火狐Selenium驅動程序已更新爲名爲Marionette或「geckodriver」的新驅動程序。這是您系統上的一個單獨的二進制文件。

瞭解如何使用Python使用geckodriver:Selenium install Marionette webdriver

如果你想使用舊硒驅動程序,版本2.x: