在vps上运行Selenium

时间:2016-01-10 06:04:31

标签: python selenium vps

我有一个简单的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,导致错误的原因是什么?

1 个答案:

答案 0 :(得分:3)

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

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

查看This link了解更多详情