使用Cron无头地运行Selenium Webdriver

时间:2014-03-13 08:05:47

标签: python-2.7 cron headless xvfb headless-browser

我正试图通过cron无头地使用Xvfb执行我的Selenium Scripts。 shell脚本的代码看起来像这样

export PATH=$PATH
Xvfb :10 -ac &
gnome-terminal -e export DISPLAY=:10 &
gnome-terminal -e firefox &
gnome-terminal -e export DISPLAY=:10 &
gnome-terminal -e firefox &
cd wait/test/
sh deploy.sh

但我得到的是这样的错误:

Failed to parse arguments: Cannot open display: 
Failed to parse arguments: Cannot open display: 
Failed to parse arguments: Cannot open display: 
Failed to parse arguments: Cannot open display: 
[dix] Could not init font path element /usr/share/fonts/X11/cyrillic, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/100dpi/:unscaled, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/75dpi/:unscaled, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/100dpi, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/75dpi, removing from list!
[dix] Could not init font path element /var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType, removing from list!
Traceback (most recent call last):
  File "runner.py", line 7, in <module>
    from test import root
  File "/home/user/Tests/redirect/wait/test/ScrapyCrawl/test.py", line 47, in <module>
    class root (unittest.TestCase):
  File "/home/user/Tests/redirect/wait/test/ScrapyCrawl/test.py", line 49, in root
    driver = webdriver.Firefox ()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 59, in __init__
    self.binary, timeout),
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 47, in __init__
    self.binary.launch_browser(self.profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 61, in launch_browser
    self._wait_until_connectable()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 100, in _wait_until_connectable
    self._get_firefox_output())
selenium.common.exceptions.WebDriverException: Message: 'The browser appears to have exited before we could connect. The output was: Error: no display specified\n' 

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:0)

我用pyvirtualdisplay来解决这个问题。它是Xvfb和Xephyr的Python包装器。更多信息可以在这里找到:pyvirtualdisplay