无法在 AWS 机器上从 python 中的 selenium 调用 firefox

新手上路,请多包涵

我正在尝试使用 python 中的 selenium 来使用 javascript 抓取一些动态页面。但是,在按照pypi页面(http://pypi.python.org/pypi/selenium)上的selenium说明进行操作后,我无法调用firefox。我在 AWS ubuntu 12.04 上安装了 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)。但我不明白解决方案,如果是的话。

谁能帮帮我吗?谢谢!

原文由 David 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 475
2 个回答

问题是 Firefox 需要显示器。我在示例中使用 了 pyvirtualdisplay 来模拟显示。解决办法是:

 from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=False, 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。

这应该可以解决您的问题。

原文由 That1Guy 发布,翻译遵循 CC BY-SA 4.0 许可协议

我也遇到过同样的问题。我使用的是 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

希望这可以帮助。

原文由 Amogh Joshi 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题