无法在 AWS 机器上的 python 中从 selenium 调用 firefox [英] unable to call firefox from selenium in python on AWS machine

查看:22
本文介绍了无法在 AWS 机器上的 python 中从 selenium 调用 firefox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 python 中的 selenium 来用 javascript 抓取一些动态页面.但是,在按照pypi页面(http://pypi.python.org/pypi/selenium)上的selenium说明进行操作后,我无法调用firefox.我在 AWS ubuntu 12.04 上安装了 Firefox.我得到的错误信息是:

I am trying to use selenium from python to scrape some dynamics pages with javascript. However, I cannot call firefox after I followed the instruction of selenium on the pypi page(http://pypi.python.org/pypi/selenium). I installed firefox on AWS ubuntu 12.04. The error message I got is:

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
'

我在网上搜索,发现这个问题发生在其他人身上(https://groups.google.com/forum/?fromgroups=#!topic/selenium-users/21sJrOJULZY).但我不明白解决方案,如果是的话.

I did search on the web and found that this problem happened with other people (https://groups.google.com/forum/?fromgroups=#!topic/selenium-users/21sJrOJULZY). But I don't understand the solution, if it is.

有人可以帮我吗?谢谢!

Can anyone help me please? Thanks!

推荐答案

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

The problem is Firefox requires a display. I've used pyvirtualdisplay in my example to simulate a display. The solution is:

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.

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

This should resolve your issue.

这篇关于无法在 AWS 机器上的 python 中从 selenium 调用 firefox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆