linux - [Solved-5 Solutions] How to run Selenium in Xvfb Linux ? - ubuntu - red hat - debian - linux server - linux pc
Linux - Problem :
Error: cannot open display: :0
click below button to copy the code. By - Linux tutorial - team
How to run Selenium in Xvfb Linux ?
Linux - Solution 1:
Open a terminal and run this command xhost +. This commands needs to be run every time you restart your machine.
Additionally make sure in your /etc/environment file there is a line
export DISPLAY=:0.0
click below button to copy the code. By - Linux tutorial - team
Linux - Solution 2:
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()
click below button to copy the code. By - Linux tutorial - team
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()
click below button to copy the code. By - Linux tutorial - team
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.
click below button to copy the code. By - Linux tutorial - team
Linux - Solution 3:
The easiest way is probably to use xvfb-run:
DISPLAY=:1 xvfb-run java -jar selenium-server-standalone-2.0b3.jar
click below button to copy the code. By - Linux tutorial - team
Linux - Solution 4:
Before running the tests, execute:
export DISPLAY=:99
/etc/init.d/xvfb start
click below button to copy the code. By - Linux tutorial - team
And after the tests:
/etc/init.d/xvfb stop
click below button to copy the code. By - Linux tutorial - team
Here you can use init.d file 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
click below button to copy the code. By - Linux tutorial - team
Linux - Solution 5:
In my case on CentOS 7:
Packages:
PIP_PACKAGES="selenium pyvirtualdisplay"
YUM_PACKAGES="python34-pip httpd firefox xorg-x11-server-Xvfb expect python34-paramiko"
click below button to copy the code. By - Linux tutorial - team
Display:
su --command="Xvfb :22 -screen 0 1024x768x16 > /dev/null 2>&1 &" root
export DISPLAY=:22
click below button to copy the code. By - Linux tutorial - team
simple test:
#!/usr/bin/env python3
from selenium import webdriver
from pyvirtualdisplay import Display
browser = webdriver.Firefox()
browser.get('https://google.com')
if "Google" in browser.title:
print(browser.title)