Run Selenium Test with Headless Browser

By | 23/08/2015

You write your test scripts and placed them in to the machine where you run. if you run on a Windows or any other O.S. has a GUI access, browser get run without problems. But, what happen if you run on remote machine by command line? For example, your scripts are in a Linux PC anf want to run via ssh consle from another PC. Most probably you will got en error. Because, browser can’t see any display to run on:

root@mahsum:~ /eclipse/workspace/PythonSelenium/src# python

ERROR: test_Mahsum_Foo (__main__.TestClass)
Traceback (most recent call last):
File "", line 19, in setUp
self.driver = webdriver.Firefox()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/", line 77, in __init__
self.binary, timeout),

File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/", line 49, in __init__
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/", line 68, in launch_browser
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/", line 98, in _wait_until_connectable
raise WebDriverException("The browser appears to have exited "

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.
Ran 1 test in 1.205s

FAILED (errors=1)

For such that sitiuation, solution is to setup virtual displays.

In Linux, there is X server. It listens for display request and create virtual displays. So, how install and configure X server for our test?

First install a X server. Currently, most using application is: xvfb

sudo apt-get install xvfb

after installation is completed, run application:

Xvfb :40 &

Most value in parameters is 40. It is display number. You can set any value betwween 0-99. You should be careful to select a number that not used by any other application. You can pass many parameters like screen resolution etc.

Xvfb :40 -screen 0 1024x768x24 -extension RANDR &

Next step is export display:

export DISPLAY=:40

as i mentioned, running display number and exported display number SHOULD be same. Another important point is, run script in command shell where you export DISPLAY. Because, it is valid only for that session/Command Shell.

Leave a Reply

Your email address will not be published. Required fields are marked *