Is it possible to run Selenium WebDriver tests in headless mode?

Yes, it is possible to run Selenium WebDriver tests in headless mode. Headless mode means running a browser session without the graphical user interface, which is especially useful for test automation on servers that do not have a display. Running tests in headless mode can also improve test execution time as it consumes fewer resources.

Below are examples of how to configure headless mode for Chrome and Firefox using Selenium WebDriver in Python and JavaScript.

Python Example

Chrome

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--headless")  # Enables headless mode
chrome_options.add_argument("--disable-gpu")  # This flag is necessary for headless mode on Windows

driver = webdriver.Chrome(options=chrome_options)

driver.get("https://example.com")
print(driver.title)

driver.quit()

Firefox

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

firefox_options = Options()
firefox_options.add_argument("--headless")  # Enables headless mode

driver = webdriver.Firefox(options=firefox_options)

driver.get("https://example.com")
print(driver.title)

driver.quit()

JavaScript Example

Chrome (with Node.js)

const { Builder } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');

let chromeOptions = new chrome.Options();
chromeOptions.headless();  // Enables headless mode

(async function example() {
    let driver = await new Builder()
        .forBrowser('chrome')
        .setChromeOptions(chromeOptions)
        .build();
    try {
        await driver.get('https://example.com');
        console.log(await driver.getTitle());
    } finally {
        await driver.quit();
    }
})();

Firefox (with Node.js)

const { Builder } = require('selenium-webdriver');
const firefox = require('selenium-webdriver/firefox');

let firefoxOptions = new firefox.Options();
firefoxOptions.headless();  // Enables headless mode

(async function example() {
    let driver = await new Builder()
        .forBrowser('firefox')
        .setFirefoxOptions(firefoxOptions)
        .build();
    try {
        await driver.get('https://example.com');
        console.log(await driver.getTitle());
    } finally {
        await driver.quit();
    }
})();

Note:

  • When running headless, it's important to ensure that your code does not rely on UI elements that would only be present if the browser was running in a normal (non-headless) mode.
  • The --disable-gpu flag is currently necessary to run Chrome headless on Windows due to a bug. But it may become unnecessary in the future as the platform evolves.
  • Always make sure that the WebDriver you are using (chromedriver for Chrome, geckodriver for Firefox) is compatible with the version of the browser installed on your system.

Running tests in headless mode is a common practice for continuous integration environments and when you don't need to visually see the actions being performed by the WebDriver.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon