Can Selenium WebDriver open browser windows silently in the background? Can Selenium WebDriver open browser windows silently in the background? selenium selenium

Can Selenium WebDriver open browser windows silently in the background?


If you are using Selenium web driver with Python, you can use PyVirtualDisplay, a Python wrapper for Xvfb and Xephyr.

PyVirtualDisplay needs Xvfb as a dependency. On Ubuntu, first install Xvfb:

sudo apt-get install xvfb

Then install PyVirtualDisplay from PyPI:

pip install pyvirtualdisplay

Sample Selenium script in Python in a headless mode with PyVirtualDisplay:

    #!/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()

EDIT

The initial answer was posted in 2014 and now we are at the cusp of 2018. Like everything else, browsers have also advanced. Chrome has a completely headless version now which eliminates the need to use any third-party libraries to hide the UI window. Sample code is as follows:

    from selenium import webdriver    from selenium.webdriver.chrome.options import Options    CHROME_PATH = '/usr/bin/google-chrome'    CHROMEDRIVER_PATH = '/usr/bin/chromedriver'    WINDOW_SIZE = "1920,1080"    chrome_options = Options()    chrome_options.add_argument("--headless")    chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE)    chrome_options.binary_location = CHROME_PATH    driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH,                              chrome_options=chrome_options                             )    driver.get("https://www.google.com")    driver.get_screenshot_as_file("capture.png")    driver.close()


There are a few ways, but it isn't a simple "set a configuration value". Unless you invest in a headless browser, which doesn't suit everyone's requirements, it is a little bit of a hack:

How to hide Firefox window (Selenium WebDriver)?

and

Is it possible to hide the browser in Selenium RC?

You can 'supposedly', pass in some parameters into Chrome, specifically: --no-startup-window

Note that for some browsers, especially Internet Explorer, it will hurt your tests to not have it run in focus.

You can also hack about a bit with AutoIt, to hide the window once it's opened.


Chrome 57 has an option to pass the --headless flag, which makes the window invisible.

This flag is different from the --no-startup-window as the last doesn't launch a window. It is used for hosting background apps, as this page says.

Java code to pass the flag to Selenium webdriver (ChromeDriver):

ChromeOptions options = new ChromeOptions();options.addArguments("--headless");ChromeDriver chromeDriver = new ChromeDriver(options);