How do I run Selenium in Xvfb?
Asked Answered
P

6

100

I'm on EC2 instance. So there is no GUI.

$pip install selenium
$sudo apt-get install firefox xvfb

Then I do this:

$Xvfb :1 -screen 0 1024x768x24 2>&1 >/dev/null &

$DISPLAY=:1 java -jar selenium-server-standalone-2.0b3.jar
05:08:31.227 INFO - Java: Sun Microsystems Inc. 19.0-b09
05:08:31.229 INFO - OS: Linux 2.6.32-305-ec2 i386
05:08:31.233 INFO - v2.0 [b3], with Core v2.0 [b3]
05:08:32.121 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
05:08:32.122 INFO - Version Jetty/5.1.x
05:08:32.123 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
05:08:32.124 INFO - Started HttpContext[/selenium-server,/selenium-server]
05:08:32.124 INFO - Started HttpContext[/,/]
05:08:32.291 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@1186fab
05:08:32.292 INFO - Started HttpContext[/wd,/wd]
05:08:32.295 INFO - Started SocketListener on 0.0.0.0:4444
05:08:32.295 INFO - Started org.openqa.jetty.jetty.Server@1ffb8dc

Great, everything should work now, right?

When I run my code:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox() 
browser.get("http://www.yahoo.com") 

I get this:

Error: cannot open display: :0
Peppy answered 31/5, 2011 at 5:18 Comment(2)
If you're on a system without X running, display :0 should be available. Try running Xvfb :0 -- also see xvfb-run as mentioned by @emaOxy
For future followers, I described my solution for Ubuntu Server: namekdev.net/2016/08/…Ruthie
E
37

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.

Ectoplasm answered 31/5, 2011 at 5:23 Comment(4)
You just need to execute java -jar selenium-server-standalone-2.0b3.jar from command line and then run your testsEctoplasm
I'm on an EC2 instance, so there is no GUI. xhost: unable to open display ":0.0"Peppy
@owa You also need the DISPLAY environment variable in the client environment.Matz
xhost + is insecure. Please do not use it. If you have to use xhost, add the specific machine you want and not + (aka everything whatsoever).Landmeier
L
183

You can use PyVirtualDisplay (a Python wrapper for Xvfb) to run headless WebDriver tests.

#!/usr/bin/env python

from pyvirtualdisplay import Display
from selenium import webdriver

display = 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.title
browser.quit()

display.stop()

more info


You can also use xvfbwrapper, which is a similar module (but has no external dependencies):

from xvfbwrapper import Xvfb

vdisplay = Xvfb()
vdisplay.start()

# launch stuff inside virtual display here

vdisplay.stop()

or better yet, use it as a context manager:

from xvfbwrapper import Xvfb

with Xvfb() as xvfb:
    # launch stuff inside virtual display here.
    # It starts/stops in this code block.
Lette answered 10/6, 2011 at 0:10 Comment(4)
@Andrei OSX doesn't use X11 by defaultTerrier
Used this answer to make it work with vagrant + PyCharm + Django on OS X: https://mcmap.net/q/122417/-setting-a-test-using-xvfb-pycharm-vagrantAmp
I get a "browser appears to have exited" when I implement the pyvirtualdisplay code abovePattani
Message: Service /usr/lib64/chromium-browser/chromium-browser unexpectedly exited. Status code was: 1Aldenalder
S
46

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!

Steppe answered 8/7, 2012 at 13:52 Comment(2)
This is just what I was looking for. Starting Xvfb on its own didn't seem to work for me, but using xvfb-run did the trick. Thanks.Broadfaced
The DISPLAY=:1 is unnecessary; xvfb-run sets the DISPLAY environment variable to something else (usually :99) before starting the specified command (in your case, java -jar selenium-server...)Renarenado
E
37

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.

Ectoplasm answered 31/5, 2011 at 5:23 Comment(4)
You just need to execute java -jar selenium-server-standalone-2.0b3.jar from command line and then run your testsEctoplasm
I'm on an EC2 instance, so there is no GUI. xhost: unable to open display ":0.0"Peppy
@owa You also need the DISPLAY environment variable in the client environment.Matz
xhost + is insecure. Please do not use it. If you have to use xhost, add the specific machine you want and not + (aka everything whatsoever).Landmeier
P
22

This is the setup I use:

Before running the tests, execute:

export DISPLAY=:99
/etc/init.d/xvfb start

And after the tests:

/etc/init.d/xvfb stop

The init.d file I use looks like this:

#!/bin/bash

XVFB=/usr/bin/Xvfb
XVFBARGS="$DISPLAY -ac -screen 0 1024x768x16"
PIDFILE=${HOME}/xvfb_${DISPLAY:1}.pid
case "$1" in
  start)
    echo -n "Starting virtual X frame buffer: Xvfb"
    /sbin/start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile --background --exec $XVFB -- $XVFBARGS
    echo "."
    ;;
  stop)
    echo -n "Stopping virtual X frame buffer: Xvfb"
    /sbin/start-stop-daemon --stop --quiet --pidfile $PIDFILE
    echo "."
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  *)
  echo "Usage: /etc/init.d/xvfb {start|stop|restart}"
  exit 1
esac
exit 0
Polynomial answered 31/5, 2011 at 6:27 Comment(2)
I did exactly this and then ran my script. However, I still get this: Error: cannot open display: :0. Also, do I need to run the selenium java server? It seems like it doesn't matter if I run it or not...Peppy
owalla, if you use WebDriver, then you don't need the Selenium Java Server.Lette
S
4

If you use Maven, you can use xvfb-maven-plugin to start xvfb before tests, run them using related DISPLAY environment variable, and stop xvfb after all.

Santanasantayana answered 22/10, 2015 at 8:51 Comment(0)
B
2

The proper way is:

xvfb-run python ./selenium-script.py

No need to add:

from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
display.stop()

But now, with Selenium WebDriver, there's no need to run also xvfb anymore (see the explanation why).

Bozeman answered 20/5, 2023 at 4:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.