How do I run Selenium in Xvfb?
You can use PyVirtualDisplay (a Python wrapper for Xvfb) to run headless WebDriver tests.
#!/usr/bin/env pythonfrom pyvirtualdisplay import Displayfrom selenium import webdriverdisplay = Display(visible=0, size=(800, 600))display.start()# now Firefox will run in a virtual display. # you will not see the browser.browser = webdriver.Firefox()browser.get('http://www.google.com')print browser.titlebrowser.quit()display.stop()
You can also use xvfbwrapper, which is a similar module (but has no external dependencies):
from xvfbwrapper import Xvfbvdisplay = Xvfb()vdisplay.start()# launch stuff inside virtual display herevdisplay.stop()
or better yet, use it as a context manager:
from xvfbwrapper import Xvfbwith Xvfb() as xvfb: # launch stuff inside virtual display here. # It starts/stops in this code block.
The easiest way is probably to use xvfb-run:
DISPLAY=:1 xvfb-run java -jar selenium-server-standalone-2.0b3.jar
xvfb-run does the whole X authority dance for you, give it a try!
open a terminal and run this command xhost +
. This commands needs to be run every time you restart your machine. If everything works fine may be you can add this to startup commands
Also make sure in your /etc/environment file there is a line
export DISPLAY=:0.0
And then, run your tests to see if your issue is resolved.
All please note the comment from sardathrion below before using this.