Is it possible to run selenium (Firefox) web driver without a GUI?

UbuntuSeleniumWebdriver

Ubuntu Problem Overview


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!

Ubuntu Solutions


Solution 1 - Ubuntu

What you're looking for is a [tag:headless-browser].

Yes, it's possible to run Selenium on Firefox headlessly. Here is a [post][1] 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

[1]: http://www.alittlemadness.com/2008/03/05/running-selenium-headless/ "Running Selenium Headless"

Solution 2 - Ubuntu

Chrome now has a headless mode:

op = webdriver.ChromeOptions()
op.add_argument('--headless')
driver = webdriver.Chrome(options=op)

Solution 3 - Ubuntu

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

Solution 4 - Ubuntu

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.

Solution 5 - Ubuntu

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.

Solution 6 - Ubuntu

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

Solution 7 - Ubuntu

Another option is GhostDriver which is now officially supported by WebDriver: https://stackoverflow.com/questions/14194371/ghostdriver-actual-performance-gain/14707494#14707494

Solution 8 - Ubuntu

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')

Solution 9 - Ubuntu

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.

Solution 10 - Ubuntu

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.

Solution 11 - Ubuntu

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnonshatterView Question on Stackoverflow
Solution 1 - UbuntugrahaminnView Answer on Stackoverflow
Solution 2 - UbuntuStéphane BruckertView Answer on Stackoverflow
Solution 3 - UbuntuA.JView Answer on Stackoverflow
Solution 4 - UbuntuharoonzoneView Answer on Stackoverflow
Solution 5 - Ubuntu郑文勋View Answer on Stackoverflow
Solution 6 - UbuntuNicholas DiPiazzaView Answer on Stackoverflow
Solution 7 - UbuntuAlister ScottView Answer on Stackoverflow
Solution 8 - UbuntuMax MalyshView Answer on Stackoverflow
Solution 9 - UbuntuguillemhsView Answer on Stackoverflow
Solution 10 - UbuntuU2647View Answer on Stackoverflow
Solution 11 - UbuntuVinee-the-PoohView Answer on Stackoverflow