【问题标题】:running Selenium on a vps [duplicate]在 vps 上运行 Selenium [重复]
【发布时间】:2016-04-14 15:27:53
【问题描述】:

我有一个简单的 python 脚本,可以在我的本地机器上正常工作。但是如果我尝试在我的 vps 上运行它,我会收到一个错误:

Traceback (most recent call last):
  File "test.py", line 15, in <module>
    browser = webdriver.Firefox(profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 77, in __init__
    self.binary, timeout),
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 49, in __init__
    self.binary.launch_browser(self.profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 68, in launch_browser
    self._wait_until_connectable()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 98, in _wait_until_connectable
    raise WebDriverException("The browser appears to have exited "
selenium.common.exceptions.WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.

这是脚本:

from selenium import webdriver

profile = webdriver.FirefoxProfile()

profile.set_preference('permissions.default.image', 2)
# Set all new windows to open in the current window instead
profile.set_preference('browser.link.open_newwindow', 1)
browser = webdriver.Firefox(profile)
browser.get('http://www.google.com/')
html = browser.page_source

print html

browser.close()

由于 vps 没有 GUI,是什么导致了错误?

【问题讨论】:

标签: python selenium vps


【解决方案1】:

您必须使用xvfb 包才能正确运行webdriver。

usr/bin/xvfb-run /usr/bin/python myfile.py

查看This link了解更多详情

【讨论】:

  • 出于好奇,您为什么认为这个问题值得回答而不是投票?
  • 公平提示,谢谢 :)
猜你喜欢
  • 2017-11-10
  • 2013-12-23
  • 1970-01-01
  • 2020-06-11
  • 2014-09-01
  • 2014-12-14
  • 2017-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多