【问题标题】:unable to call firefox from selenium in python on AWS machine无法从 AWS 机器上的 python 中的 selenium 调用 firefox
【发布时间】:2012-10-13 22:06:51
【问题描述】:

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

谁能帮帮我?谢谢!

【问题讨论】:

  • Error: no display specified 表示浏览器没有屏幕来显示其主窗口。您需要找到一种无头运行 Firefox 的方法:stackoverflow.com/questions/10060417/python-firefox-headless。这个答案特别有用:stackoverflow.com/a/6300672/464744
  • @Blender 非常感谢。第二个链接解决了我的问题。有时,如果我的脑海中没有正确的关键字,我就无法从谷歌找到解决方案。
  • @Blender:你是如何在你的 URL 中获得一个对页面响应的锚点?我在页面中没有看到这样的链接。
  • @sputnick 查看“共享”href。

标签: python selenium amazon-web-services screen-scraping web-scraping


【解决方案1】:

对于 Debian 10 和 Ubuntu 18.04,这是一个完整的运行示例:

  1. 在 ~/Downloads 中下载 Chrome 驱动程序:
    $ wget https://chromedriver.storage.googleapis.com/80.0.3987.16/chromedriver_linux64.zip

  2. unzip chromedriver_linux64.zip解压

  3. 将文件移动到可执行文件夹(已包含路径):
    $ sudo mv chromedriver /usr/local/bin

然后在带有 Jupyter 的笔记本或脚本中运行此代码:

from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.set_headless(headless=True)


browser = Chrome(chrome_options=chrome_options)
browser.get('http://www.linkedin.com/')
print(browser.page_source)

这将打印页面中的整个源 HTML。

【讨论】:

    【解决方案2】:

    这已经在OP问题的评论中,但将其作为答案。您可以让 Selenium 在后台运行,而无需打开实际的浏览器窗口。

    例如,如果您使用 Chrome,请设置以下选项:

    from selenium.webdriver.chrome.options import Options
    chrome_options = Options()
    chrome_options.set_headless(headless=True)
    

    那么当你调用你的网络驱动时,你的设置就变成了一个参数:

    browser = webdriver.Chrome(chrome_options=chrome_options)
    

    【讨论】:

    • 你为什么设置为假?
    • 谢谢。改为真。
    【解决方案3】:

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

    希望这会有所帮助。

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      • 谢谢。一个简单的问题:driver.close() 和 driver.quite() 有什么区别?
      • 以帮助将来偶然发现此问题的人们.. 请注意,将关闭/退出与 @That1Guy 区分开来的评论是完全错误的。另请注意,他的答案中的代码永远不会正确关闭底层驱动程序,并且可能会泄漏进程或文件描述符。 driver.close() 只是关闭当前窗口。它将保持其他窗口打开并且驱动程序处于活动状态。 driver.quit() 实际上会退出驱动程序并关闭每个关联的窗口。如果您想了解更多关于差异的详细信息,请阅读 selenium webdriver 源代码。
      • 给未来读者的说明:@CoreyGoldberg 所指的评论实际上是不正确的。我不仅混淆了我提到的 Selenium 方法,还混淆了我当时正在从事的另一个项目。请参阅driver.quit()drver.close() 的文档。
      • @gvrocha 我没有测试过,但我不明白为什么pyvirtualdisplay 不能在 Mac 上工作。
      • 我尝试了上述步骤。但是,我仍然得到同样的错误。任何人都可以帮忙吗?我通过运行 sudo apt-get install xvfb 安装了 xvfb。然后尝试运行此答案中提到的脚本。但是,我仍然收到相同的错误“selenium.common.exceptions.WebDriverException:消息:浏览器似乎在我们可以连接之前已经退出......”
      猜你喜欢
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      • 2017-05-08
      • 2018-01-23
      • 2018-01-02
      • 1970-01-01
      • 2017-05-24
      • 1970-01-01
      相关资源
      最近更新 更多