0
0
Selenium-pythonHow-ToBeginner ยท 3 min read

How to Use Headless Browser with Selenium in Python

To use a headless browser with Selenium in Python, configure browser options to run without a GUI by adding the --headless argument. For example, with Chrome, use Options() to add --headless before creating the WebDriver instance.
๐Ÿ“

Syntax

To run Selenium in headless mode, you create browser-specific options and add the --headless argument. Then, pass these options when initializing the WebDriver.

  • Options(): Create browser options object.
  • add_argument('--headless'): Enables headless mode.
  • WebDriver(options=options): Starts browser with these options.
python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)

# Use driver as usual

# Quit when done
driver.quit()
๐Ÿ’ป

Example

This example opens a headless Chrome browser, navigates to example.com, prints the page title, and then closes the browser. It shows how to run Selenium without opening a visible browser window.

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

options = Options()
options.add_argument('--headless=new')
options.add_argument('--disable-gpu')  # Recommended for Windows

with webdriver.Chrome(options=options) as driver:
    driver.get('https://example.com')
    print(driver.title)
Output
Example Domain
โš ๏ธ

Common Pitfalls

Common mistakes when using headless mode include:

  • Not adding --headless argument, so browser opens normally.
  • Missing other recommended flags like --disable-gpu on Windows, which can cause errors.
  • Assuming headless mode behaves exactly like headed mode; some UI elements may load differently.
  • Not closing the driver properly, causing resource leaks.

Always test your scripts in both headed and headless modes to catch differences.

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

# Wrong: missing headless argument
options_wrong = Options()
# options_wrong.add_argument('--headless')  # Forgot this

# Right: add headless argument
options_right = Options()
options_right.add_argument('--headless=new')
options_right.add_argument('--disable-gpu')

# Use options_right when creating driver
๐Ÿ“Š

Quick Reference

StepDescriptionExample Code Snippet
1Import Selenium and Optionsfrom selenium import webdriver from selenium.webdriver.chrome.options import Options
2Create Options and add headlessoptions = Options() options.add_argument('--headless=new')
3Create WebDriver with optionsdriver = webdriver.Chrome(options=options)
4Use driver as usualdriver.get('https://example.com')
5Close driverdriver.quit()
โœ…

Key Takeaways

Add '--headless' argument to browser options to run Selenium without opening a browser window.
Include '--disable-gpu' on Windows to avoid common headless mode issues.
Test your scripts in both headed and headless modes to ensure consistent behavior.
Always close the WebDriver to free system resources.
Headless mode speeds up tests by removing the UI but may behave slightly differently.