Using Selenium Python with Chrome in Headless Mode

Selenium is a powerful tool for web automation, widely used for testing web applications and automating browser interactions. However, running tests with a visible browser can slow down performance and isn't suitable for environments without a GUI (like CI/CD servers or cloud platforms). That's where headless mode comes in—allowing you to run tests without opening a browser window.

In this blog, you'll learn how to use Selenium with Python and Google Chrome in headless mode, understand the benefits, and explore practical use cases.


✅ What is Headless Mode?

Headless mode means running a browser without its graphical interface. It behaves exactly like a regular browser but operates entirely in the background. This mode is particularly useful when:

You want to speed up automated tests

You're running tests on a server or CI pipeline without a display

You need to run multiple browser instances in parallel efficiently


๐Ÿ”ง Setting Up Selenium with Chrome in Headless Mode

Before writing the script, make sure you have the following:

๐Ÿ“ฆ 1. Install Selenium

If you haven't installed Selenium yet, do it using pip:


bash

pip install selenium

๐Ÿ“ฅ 2. Download ChromeDriver

Visit https://chromedriver.chromium.org/downloads

Download the version that matches your Chrome browser

Place the executable in your system’s PATH or specify the path in your script


๐Ÿงช Example: Running Chrome in Headless Mode with Selenium

Here’s a simple Python script to launch Chrome in headless mode and perform basic interactions:


python

from selenium import webdriver

from selenium.webdriver.chrome.options import Options

from selenium.webdriver.common.by import By


# Setup Chrome options

options = Options()

options.headless = True  # Enable headless mode

options.add_argument("--disable-gpu")

options.add_argument("--window-size=1920,1080")


# Launch Chrome browser in headless mode

driver = webdriver.Chrome(options=options)


# Navigate to a webpage

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


# Print page title

print("Page Title:", driver.title)


# Interact with an element (example: get heading text)

heading = driver.find_element(By.TAG_NAME, "h1")

print("Heading:", heading.text)


# Take screenshot in headless mode

driver.save_screenshot("headless_example.png")


# Close the browser

driver.quit()

๐Ÿงฐ When to Use Headless Mode

CI/CD pipelines: Run tests automatically without opening a browser window.

Web scraping: Collect data quickly without rendering the GUI.

Smoke testing: Validate essential features in seconds.

Cloud execution: Ideal for running scripts on servers or Docker containers.


⚠️ Tips & Best Practices

Always set a window size using --window-size=1920,1080 for consistent rendering.

Some JavaScript-heavy websites might behave differently in headless mode—test thoroughly.

You can still use features like screenshots and HTML source extraction.


๐Ÿ“Œ Real-World Use Case

Imagine you're validating whether a login page works correctly:

python


driver.get("https://example.com/login")

driver.find_element(By.ID, "username").send_keys("testuser")

driver.find_element(By.ID, "password").send_keys("securepass")

driver.find_element(By.ID, "submit").click()

print(driver.current_url)  # Should redirect to dashboard

In headless mode, this test will run faster and won't require any GUI.


๐Ÿ Conclusion

Using Chrome in headless mode with Selenium Python is an efficient way to perform browser automation, especially when running in non-GUI environments or speeding up test execution. It's a best practice in modern DevOps and QA workflows and easy to implement with just a few lines of code.

Whether you're testing applications or scraping data, headless mode is a must-have tool in your Selenium skillset.


Learn  Selenium with Python Training Course

Read More: Parallel Testing in Selenium Python with PyTest

Read More: Validating Page Titles in Selenium Python
Read More: Automating Login and Logout with Selenium Python

Visit Quality Thought Training Institute in Hyderabad
Get Direction 

Comments

Popular posts from this blog

Tosca vs Selenium: Which One to Choose?

Flask API Optimization: Using Content Delivery Networks (CDNs)

Using ID and Name Locators in Selenium Python