小编典典

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

selenium

我正在尝试使用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)。但是我不理解解决方案,如果可以的话。

谁能帮我吗?谢谢!


阅读 286

收藏
2020-06-26

共1个答案

小编典典

问题是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。

这样可以解决您的问题。

2020-06-26