0
0
Selenium Pythontesting~5 mins

Headless mode for CI in Selenium Python

Choose your learning style9 modes available
Introduction

Headless mode lets browsers run without showing a window. This helps tests run faster and work well on servers without screens.

When running automated tests on a server without a display.
To speed up test execution by skipping the browser UI.
When integrating tests into Continuous Integration (CI) pipelines.
To save system resources during large test runs.
When you want to run tests remotely without opening browser windows.
Syntax
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.headless = True

driver = webdriver.Chrome(options=options)

Set options.headless = True before creating the browser driver.

This example uses Chrome; other browsers have similar options.

Examples
This runs Chrome in headless mode, opens a page, prints the title, then closes the browser.
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.headless = True

driver = webdriver.Chrome(options=options)
driver.get('https://example.com')
print(driver.title)
driver.quit()
This runs Firefox in headless mode for the same steps.
Selenium Python
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.headless = True

driver = webdriver.Firefox(options=options)
driver.get('https://example.com')
print(driver.title)
driver.quit()
Sample Program

This script runs Chrome in headless mode, opens the Python homepage, prints the page title, then closes the browser.

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

options = Options()
options.headless = True

driver = webdriver.Chrome(options=options)

driver.get('https://www.python.org')
print(driver.title)

driver.quit()
OutputSuccess
Important Notes

Headless mode may behave slightly differently than normal mode; always test both if possible.

Some websites detect headless browsers and may block them; use user-agent strings if needed.

Make sure your CI environment has the browser and drivers installed properly.

Summary

Headless mode runs browsers without a visible window, ideal for CI servers.

It speeds up tests and saves resources.

Use browser options to enable headless mode before starting tests.