125

We are considering upgrading our production server from Ubuntu-desktop 10.04 to Ubuntu-server 12.04.

We have various services running on our current desktop OS such as Selenium Web Driver. My question is can the Selenium Web Driver be run from a cli-based system?

My immediate thought is that it can't, because it relies on Firefox, but I'd like for someone to prove me wrong!

josliber
  • 43,891
  • 12
  • 98
  • 133
nonshatter
  • 3,347
  • 6
  • 23
  • 27
  • related: [Running Headless Selenium with Chrome](http://www.chrisle.me/2013/08/running-headless-selenium-with-chrome/) – jfs Mar 05 '14 at 16:15

11 Answers11

116

What you're looking for is a .

Yes, it's possible to run Selenium on Firefox headlessly. Here is a post you can follow.

Here is the summary steps to set up Xvfb

#install Xvfb
sudo apt-get install xvfb

#set display number to :99
Xvfb :99 -ac &
export DISPLAY=:99    

#you are now having an X display by Xvfb
Nam G VU
  • 33,193
  • 69
  • 233
  • 372
grahaminn
  • 1,557
  • 1
  • 10
  • 10
  • 5
    This works and supports screenshots. I use this. It also works with google chrome. – Isaac May 02 '12 at 14:30
  • I don't think this works anymore (at least not easily, because it requires a new geckodriver much like chrome did) – Archimedes Trajano Nov 02 '16 at 19:12
  • 1
    @ArchimedesTrajano This still works. The directions are for creating a virtual display using `Xvfb`, not for installing and configuring Selenium (which requires having `geckodriver` on the path). – expz Nov 12 '16 at 02:05
  • I am having issues on all Linux OS except Ubuntu Desktop. Any ideas? Sometimes it says that it can't start Chrome, sometimes that there is timeout – Toolkit Sep 07 '17 at 19:16
  • 3
    I like doing this from within Python, which you can do with `subprocess.Popen('Xvfb...')` or `os.system('Xvfb...')`, but make sure to do it before importing the webdriver. – wordsforthewise Oct 11 '17 at 04:59
  • I don't understand why we need Xvfb if we are using `headless-browser` – Darshan Dec 05 '17 at 21:49
  • 11
    that link is now broken – oldboy Jun 23 '18 at 02:34
  • 1
    `xvfb-run` - run specified X client or command in a virtual X server environment – Hritik Nov 09 '19 at 19:23
39

Chrome now has a headless mode:

op = webdriver.ChromeOptions()
op.add_argument('--headless')
driver = webdriver.Chrome(options=op)
Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
11

Yes. You can use HTMLUnitDriver instead for FirefoxDriver while starting webdriver. This is headless browser setup. Details can be found here.

Priidu Neemre
  • 2,813
  • 2
  • 39
  • 40
A.J
  • 4,929
  • 2
  • 27
  • 36
  • 1
    Would this support functions such as save_screenshot()? http://coreygoldberg.blogspot.co.uk/2011/06/python-selenium-webdriver-capture.html – nonshatter May 01 '12 at 15:05
  • Haven't tried, but it may be possible because you're effectively still creating a UI, but showing it on a 'virtual' window. – grahaminn May 01 '12 at 15:06
  • 2
    HTMLUnitDriver will NOT support capture screenshot as content is not rendered at all (Issue 1361). An alternative I can think of is to use getHTMLSource and show the HTML page rather than screenshot. – A.J May 01 '12 at 15:11
10

If you want headless browser support then there is another approach you might adopt.

https://github.com/detro/ghostdriver

It was announced during Selenium Conference and it is still in development. It uses PhantomJS as the browser and is much better than HTMLUnitDriver, there are no screenshots yet, but as it is still in active development.

haroonzone
  • 374
  • 1
  • 4
8

An optional is to use pyvirtualdisplay like this:

from pyvirtualdisplay import Display

display = Display(visible=0, size=[800, 600])
display.start()

#do selenium job here

display.close()

A shorter version is:

with Display() as display:
    # selenium job here

This is generally a python encapsulate of xvfb, and more convinient somehow.

By the way, although PhantomJS is a headless browser and no window will be open if you use it, it seems that PhantomJS still needs a gui environment to work.

I got Error Code -6 when I use PhantomJS() instead of Firefox() in headless mode (putty-connected console). However everything is ok in desktop environment.

郑文勋
  • 89
  • 1
  • 3
4

UPDATE: You do not need XVFB to run headless Firefox anymore. Firefox v55+ on Linux and Firefox v56+ on Windows/Mac now supports headless execution.

I added some how-to-use documentation here:

https://developer.mozilla.org/en-US/Firefox/Headless_mode#Selenium_in_Java

Nicholas DiPiazza
  • 10,029
  • 11
  • 83
  • 152
  • 1
    Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/173662/discussion-on-answer-by-nicholas-dipiazza-is-it-possible-to-run-selenium-firefo). – Bhargav Rao Jun 23 '18 at 06:40
  • This didn't work for me. I'm not sure, but I think XVFB is still required. – Max Malysh Jan 31 '20 at 19:10
  • I've been using headless browsers on Firefox and Chrome for several years now. The WebDriver interface for Selenium even supports a setHeadless parameter now. – Nicholas DiPiazza Jan 31 '20 at 20:16
4

Install & run containerized Firefox:

docker pull selenium/standalone-firefox
docker run --rm -d -p 4444:4444 --shm-size=2g selenium/standalone-firefox

Connect using webdriver.Remote:

driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX)
driver.set_window_size(1280, 1024)
driver.get('https://www.google.com')
Max Malysh
  • 29,384
  • 19
  • 111
  • 115
2

Another option is GhostDriver which is now officially supported by WebDriver: Ghostdriver actual performance gain

Community
  • 1
  • 1
Alister Scott
  • 3,675
  • 24
  • 41
1

Be aware that HtmlUnitDriver webclient is single-threaded and Ghostdriver is only at 40% of the functionalities to be a WebDriver.

Nonetheless, Ghostdriver run properly for tests and I have problems to connect it to the WebDriver hub.

guillemhs
  • 330
  • 1
  • 2
  • 17
1

maybe you need to set your window-size dimension. just like:

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--window-size=1920x1080');

browser = webdriver.Chrome(options=options,executable_path = './chromedriver')

if also not working, try increase window-size dimension.

U2647
  • 450
  • 4
  • 10
0

Yes, you can run test scripts without a browser, But you should run them in headless mode.

Vinee-the-Pooh
  • 835
  • 3
  • 10
  • 27