A low-code platform blending no-code simplicity with full-code power 🚀
Get started free
March 2, 2025
•
5
min read

Using Headless Browsers with Selenium: Setup and Code Examples

George Miloradovich
Researcher, Copywriter & Usecase Interviewer
Table of contents

Headless browsers let you run automated web interactions faster by skipping the graphical interface. This makes them ideal for testing, scraping, and form automation. Here's what you need to know:

  • Why Use Headless Mode?
    • Speed: 2–15× faster than regular browsers.
    • Low Resource Usage: Minimal memory and CPU consumption.
    • Use Cases: Automated testing, web scraping, and CI/CD workflows.
  • Setup Requirements:
    • Tools Needed: Python (v3.7+), Selenium, browser drivers, and compatible browsers (Chrome v109+, Firefox, Edge).
    • Installation: Use pip install selenium and configure headless mode with simple arguments like --headless=new.
  • Key Tips for Success:
    • Set fixed window sizes for consistent results.
    • Use explicit waits for dynamic content.
    • Debug with screenshots and logs.

Quick Comparison

Feature Headless Mode Standard Browser
Speed 2–15× faster Standard speed
Resource Usage Low High
Visual Feedback None Full GUI
Automation Easier integration More complex setup

Headless browsers are powerful for fast, efficient automation. Follow the setup steps and best practices to streamline your workflow.

Selenium x Python, to automate Headless Browser

Selenium

Setup Guide for Selenium Headless Browsers

Here's how you can set up Selenium for headless browser automation.

Install Required Components

You'll need the following components installed:

Component Version Requirements Purpose
Python 3.7 or higher Programming environment
Selenium WebDriver Latest stable release Browser automation framework
Browser Drivers Latest stable release Controllers for specific browsers
Web Browsers Chrome (v109+ for headless mode), Firefox, Edge Target browsers for automation

Tip: Make sure the browser drivers match your browser versions to avoid compatibility issues.

Set Up Python Environment

Create a dedicated Python environment for your project:

python -m venv selenium_env
source selenium_env/bin/activate  # For Unix/macOS
selenium_env\Scripts\activate    # For Windows

"Selenium automates browsers. That's it!" - selenium.dev

Once your environment is ready, install the necessary Python libraries.

Required Python Libraries

Use pip to install the required packages:

pip install selenium
pip install webdriver_manager  # Simplifies driver management

For Chrome's headless mode (v109+), configure it like this:

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

chrome_options = Options()
chrome_options.add_argument("--headless=new")
driver = webdriver.Chrome(options=chrome_options)

On Linux, include these additional arguments to avoid common issues:

chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")

Finally, always remember to close the WebDriver instance to free up resources:

driver.quit()

Follow these steps to get your Selenium headless automation up and running!

sbb-itb-23997f1

Using Headless Browsers in Selenium

Chrome Headless Mode Setup

Chrome

For Chrome (v109+), activate headless mode by using the --headless flag as explained earlier.

Firefox Headless Mode Setup

Firefox

To enable headless mode in Firefox with GeckoDriver, use the following code:

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

firefox_options = Options()
firefox_options.add_argument("--headless")
driver = webdriver.Firefox(options=firefox_options)

Edge Headless Mode Setup

Edge

Edge also supports headless mode, and the setup is similar to Chrome. Here's an example:

from selenium import webdriver
from selenium.webdriver.edge.options import Options

edge_options = Options()
edge_options.add_argument("--headless")
driver = webdriver.Edge(options=edge_options)

"Headless is an execution mode for Firefox and Chromium based browsers. It allows users to run automated scripts in headless mode, meaning that the browser window wouldn't be visible." - Diego Molina, Selenium

Make sure to call driver.quit() after your tests to release system resources.

Use these browser-specific configurations along with the upcoming best practices to ensure smooth automation. Stay tuned for tips on optimizing your workflow and resolving common issues.

Tips and Problem-Solving Guide

After setting up your headless browsers, use these strategies to ensure smooth and effective automation.

Automation Success Tips

Fine-tune your automation process with proper settings and timing adjustments.

  • Set a fixed browser window size: Use 1920x1080 to ensure consistent element rendering across pages.
  • Handle dynamic content with explicit waits: This helps avoid timing issues when elements load asynchronously. Here's an example:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "dynamic-content"))
)
  • Incorporate scrolling for hidden elements: This is especially useful for pages that load content dynamically as you scroll.
driver.execute_script("window.scrollBy(0, 100);")

Fix Common Problems

Headless mode can sometimes cause issues like element detection failures or unexpected behavior. Here's how to address these:

  • Element Not Found: Ensure the page has fully loaded. You can check this by verifying the current URL:
current_url = driver.current_url

Also, try setting a custom user-agent or adding retry logic to handle intermittent failures.

  • Click Events Not Working: If standard click methods fail, use JavaScript to trigger the click action:
element = driver.find_element(By.ID, "button-id")
driver.execute_script("arguments[0].click();", element)

Debug Automation Scripts

When headless scripts don't work as expected, debugging is key. Here are some methods to troubleshoot:

  • Capture Screenshots: Save screenshots at critical points to identify visual issues.
driver.save_screenshot("debug_screenshot.png")
  • Implement Logging: Add detailed logs to track script execution and identify failures.
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
logger.debug('Clicking element: button-1')
  • Enable Remote Debugging: Use Chrome's remote debugging feature to inspect the browser during execution.
chrome_options.add_argument("--remote-debugging-port=9222")

Best Practices for Maintenance

To keep your tests reliable over time:

  • Store test data in external files to avoid hardcoding.
  • Use assertions to verify key checkpoints in your scripts.
  • Regularly update locators to match changes in the web application.
  • Maintain a clear and consistent project structure to simplify updates and debugging.

Summary

This section brings together the key advantages of Selenium headless automation and provides actionable steps to help you get started.

Key Uses and Advantages

Selenium headless automation speeds up testing - up to 15× faster - by skipping GUI rendering. This approach reduces resource consumption and simplifies CI/CD workflows .

Headless browsers are particularly useful for:

  • Continuous Integration: Easily integrate into CI/CD pipelines without relying on GUIs.
  • Resource Efficiency: Use less memory and CPU, enabling multiple tests to run simultaneously.
  • Web Scraping: Extract data effectively from JavaScript-heavy websites.
  • Network Performance Testing: Monitor application behavior and response times.

Next Steps

To make the most of these benefits, consider the following recommendations:

Browser Best For Key Consideration
Chrome Headless General automation and testing Broad support and compatibility
Firefox Headless Cross-browser validation Strong compliance with standards

Implementation Tips:

  • Configure WebDriver with headless options specific to your browser.
  • Set appropriate window sizes and timeouts.
  • Use wait conditions to handle dynamic content.
  • Enable screenshot functionality for debugging purposes.
  • Integrate with CI/CD tools like Jenkins for streamlined workflows.

"Headless browser testing is useful for verifying text, elements, or other static data on a certain webpage. Just like a regular browser, a headless browser understands HTML and CSS. It can also execute JavaScript like AJAX requests."

Related Blog Posts

Related Blogs

Use case

Backed by