- Selenium Features
- Selenium Installation
- Selenium Locators
- XPath Fundamentals
- CSS Selectors Methods
- Different Browsers Execution
- find_element & find_elements
- Check Enabled Status
- Check Displayed Status
- Check Selected Status
- Selenium Waits
- Send_keys Method
- Click Method
- Get Text
- Get Attribute Value
- Get Current URL
- Forward, Back, Refresh
- Take Screenshot
- Handle Browser Tabs
- Handle iframe
- Mouse Hover
- Context-Click
- Drag & Drop
- Handle Alerts
- Handle Dropdown
- Execute Javascript
- Scroll To element
- Headless Mode Execution
- Chrome Options
- Keyboard Action
Introduction to Headless Mode in Selenium
While Selenium can execute scripts with a visible browser window, headless execution allows tests to run in the background, significantly improving performance and efficiency.
Headless mode is particularly useful for environments where GUI is not required, such as server-side automation, continuous integration (CI/CD) pipelines, and web scraping. This article provides a comprehensive guide on how to execute Selenium scripts in headless mode using Python.
Why Use Headless Mode in Selenium?
There are several benefits to using headless mode in Selenium:
- Increased Performance: Without rendering a UI, execution is faster and consumes fewer system resources.
- Automation in Non-GUI Environments: Essential for running tests on headless servers.
- Enhanced Stability in CI/CD Pipelines: Minimizes issues related to UI rendering.
- Faster Web Scraping: Extracts data from websites efficiently without opening a visible browser window.
Setting Up Selenium for Headless Execution
Before running Selenium in headless mode, ensure you have the following installed:
Headless Mode Execution with Chrome
Google Chrome supports headless execution natively. Below is how you can configure Selenium with Chrome in headless mode:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# Set up Chrome options
chrome_options = Options()
chrome_options.add_argument("--headless") # Enable headless mode
chrome_options.add_argument("--disable-gpu") # Recommended for Windows OS
chrome_options.add_argument("--window-size=1920x1080") # Set window size
driver = webdriver.Chrome(options=chrome_options)
# Open a website
driver.get("https://www.facebook.com")
print(driver.title)
# Close the browser
driver.quit()
Headless Mode Execution with Firefox (GeckoDriver)
For Mozilla Firefox, you need to set the headless argument:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
# Set up Firefox options
firefox_options = Options()
firefox_options.add_argument("--headless")
driver = webdriver.Firefox(options=firefox_options)
# Open a website
driver.get("https://www.facebook.com")
print(driver.title)
# Close the browser
driver.quit()
Running Selenium in Headless Mode with Different Browsers
Apart from Chrome and Firefox, Selenium also supports headless execution with other browsers like Edge and Opera. Here’s how you can configure them:
Microsoft Edge (Chromium-based)
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)
# Open a website
driver.get("https://www.facebook.com")
print(driver.title)
# Close the browser
driver.quit()
Taking Screenshots in Headless Mode
Even though the browser is running in headless mode, you can still capture screenshots:
driver.save_screenshot("screenshot.png")
Best Practices for Running Selenium in Headless Mode
- Always set a window size: Some elements may not load correctly without a defined viewport.
- Use explicit waits: JavaScript-heavy pages require WebDriverWait.
- Handle exceptions: Use
try-except
to manage errors efficiently. - Use logging: Monitor script behavior with
logging
. - Update drivers: Ensure compatibility with the latest browser versions.