Selenium testing without browser

Each Answer to this Q is separated by one/two green lines.

I use Selenium RC for testing. Now to perform a load test, I want to run parallel test cases.
Is there any way to run them without opening a browser?

Chrome now has a headless mode:

op = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=op)

Try this code:

op = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=op)

To set up on Centos (do all installation as root)

Install pip Download


Installing selenium
If you have pip on your system, you can simply install or upgrade the Python bindings:
pip install -U selenium

Alternately, you can download the source distribution from PyPI (e.g. selenium-2.53.1.tar.gz), unarchive it, and run:

python install

install the program: pyvirtualdisplay

pip install pyvirtualdisplay

yum install Xvfb libXfont Xorg

Then modify your script to add the bold lines within ** and **

**from pyvirtualdisplay import Display**
from selenium import webdriver
from import By
from selenium.webdriver.common.keys import Keys
from import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class SeleniumDemo(unittest.TestCase):

    def setUp(self):
        **self.display = Display(visible=0, size=(800, 600))
        self.driver = webdriver.Firefox()
        self.base_url = ""
        self.verificationErrors = []
        self.accept_next_alert = True

    def tearDown(self):`enter code here`
        self.assertEqual([], self.verificationErrors)

You can run Selenium headless, take a look at this question/answer: Is it possible to hide the browser in Selenium RC?

Especially for performance load tests, you should have a look at
Apache JMeter.

Since PhantomJS has been deprecated, using headless versions of Firefox would be a viable option.

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
driver = webdriver.Firefox(options=options)

Always follow the Documentation. Here is what selenium doc says. It provide a standalone jar.

  • Download the standalone jar. And run it with command

    java -jar selenium-server-standalone.jar
  • Now you will see a stanalone server started.

  • Now set up your webdriver like below and rest part will be as it is.

    driver = webdriver.Remote(command_executor="", desired_capabilities={'browserName': 'htmlunit', 'version': '2', 'javascriptEnabled': True})
  • Summary code will be like.

    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    from selenium.webdriver.common.keys import Keys
    driver = webdriver.Remote(command_executor="", 
    desired_capabilities={'browserName': 'htmlunit', 'version': '2', 
    'javascriptEnabled': True})
    assert "Python" in driver.title
    elem = driver.find_element_by_name("q")
    assert "No results found." not in driver.page_source

It is possible, but not with the standard firefox driver / chrome / etc.

You would need to install PhantomJS. Just assign your WebDriver to an instance of phantomJS driver:

driver = webdriver.PhantomJS()

If you run your code now, no browser window will be opened.

You can import Options if you don’t want to open a web browser.

from selenium import webdriver   # for webdriver
from import WebDriverWait  # for implicit and explict waits
from import Options  # for suppressing the browser

Then in the code:

option = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=option)

And continue with the rest of the program.


sudo apt-get install xvfb
pip install selenium
pip install PyVirtualDisplay

download chrome driver binary from below link and paste into drivers directory:


from selenium import webdriver
from pyvirtualdisplay import Display

with Display(visible=False, size=(800, 600)):
    browser = webdriver.Chrome('drivers/chromedriver')

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .