27

我正嘗試使用python中的selenium來使用javascript來刮取一些動態頁面。但是,在我遵循pypi頁面上的硒指令(http://pypi.python.org/pypi/selenium)之後,我無法調用firefox。我在AWS ubuntu 12.04上安裝了firefox。我得到的錯誤信息是:無法從AWS上的python中調用selenium的firefox

In [1]: from selenium import webdriver 

In [2]: br = webdriver.Firefox() 
--------------------------------------------------------------------------- 
WebDriverException      Traceback (most recent call last) 
/home/ubuntu/<ipython-input-2-d6a5d754ea44> in <module>() 
----> 1 br = webdriver.Firefox() 

/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.pyc in __init__(self, firefox_profile, firefox_binary, timeout) 
    49   RemoteWebDriver.__init__(self, 
    50    command_executor=ExtensionConnection("127.0.0.1", self.profile, 
---> 51    self.binary, timeout), 
    52    desired_capabilities=DesiredCapabilities.FIREFOX) 
    53 

/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.pyc in __init__(self, host, firefox_profile, firefox_binary, timeout) 
    45   self.profile.add_extension() 
    46 
---> 47   self.binary.launch_browser(self.profile) 
    48   _URL = "http://%s:%d/hub" % (HOST, PORT) 
    49   RemoteConnection.__init__(

/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.pyc in launch_browser(self, profile) 
    42 
    43   self._start_from_profile_path(self.profile.path) 
---> 44   self._wait_until_connectable() 
    45 
    46  def kill(self): 

/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.pyc in _wait_until_connectable(self) 
    79     raise WebDriverException("The browser appears to have exited " 
    80      "before we could connect. The output was: %s" % 
---> 81      self._get_firefox_output()) 
    82    if count == 30: 
    83     self.kill() 

WebDriverException: Message: 'The browser appears to have exited before we could connect. The output was: Error: no display specified\n' 

我沒有在網絡上搜索,發現這個問題發生的與其他人(https://groups.google.com/forum/?fromgroups=#!topic/selenium -users/21sJrOJULZY)。但如果是這樣,我不明白解決方案。

誰能幫助我嗎?謝謝!

+4

'錯誤:未指定顯示「表示瀏覽器沒有顯示其主窗口的屏幕。您需要找到一種運行Firefox無頭的方法:http://stackoverflow.com/questions/10060417/python-firefox-headless。這個答案特別看起來很有用:http://stackoverflow.com/a/6300672/464744 – Blender

+0

@Blender非常感謝你。第二個鏈接解決了我的問題。有時候,如果我沒有正確的關鍵字,我就無法從google找到解決方案。 – David

+0

@Blender:你是如何在網頁中獲得錨點以便在頁面中響應的?我在頁面中看不到像這樣的鏈接。 –

回答

51

問題是Firefox需要顯示器。我在示例中使用了pyvirtualdisplay來模擬顯示。解決的辦法是:

from pyvirtualdisplay import Display 
from selenium import webdriver 

display = Display(visible=0, size=(1024, 768)) 
display.start() 

driver= webdriver.Firefox() 
driver.get("http://www.somewebsite.com/") 

<---some code---> 

#driver.close() # Close the current window. 
driver.quit() # Quit the driver and close every associated window. 
display.stop() 

請注意,pyvirtualdisplay需要以下後端之一:Xvfb來,Xephyr,Xvnc的。

這應該可以解決您的問題。

+0

謝謝。一個簡單的問題:driver.close()和driver.quite()有什麼區別? – David

+1

幫助未來的人們偶然發現這一點..請注意區分關閉/退出@ That1Guy的評論顯然是錯誤的。還要注意他的答案中的代碼從未正確關閉底層驅動程序,並可能泄漏進程或文件描述符。 'driver.close()'只是關閉當前窗口。它會讓其他窗口打開並且驅動程序處於活動狀態。 'driver.quit()'實際上會退出驅動程序並關閉每個關聯的窗口。 如果您想了解更多關於差異的細節,請閱讀selenium webdriver源代碼。 –

+1

給未來讀者的一封信:@CoreyGoldberg所說的評論其實是不正確的。我不僅困惑了我提到的Selenium的方法,而且還困惑於當時正在研究的另一個項目。請參考['driver.quit()']的文檔(http://selenium-python.readthedocs.org/api.html?highlight=driver.quit#selenium.webdriver.firefox.webdriver.WebDriver.quit)和['drver.close()'](http://selenium-python.readthedocs.org/api.html?highlight=driver.close#selenium.webdriver.remote.webdriver.WebDriver.close)。 – That1Guy

4

我也遇到了同樣的問題。我在Firefox 47和Selenium 2.53上。所以我所做的就是將Firefox降級到45。

1)刪除Firefox的47首:

sudo apt-get purge firefox

2)檢查可用的版本:

apt-cache show firefox | grep Version

它會顯示可用的Firefox版本,如:

Version: 47.0+build3-0ubuntu0.16.04.1

Version: 45.0.2+build1-0ubuntu1

3)告訴其構建下載

sudo apt-get install firefox=45.0.2+build1-0ubuntu1

4)接下來,您必須不再次升級到新版本。

sudo apt-mark hold firefox

5)如果你想升級後

sudo apt-mark unhold firefox sudo apt-get upgrade

希望這有助於。

相關問題