Selenium is one of the most powerful tools for web automation testing. Python can run this tool using the Selenium module, which can be installed from the PyPI.
By default, Python selenium performs automation tests by opening the browser GUI, but it can also be run in the background without opening the GUI. In the second case, we say selenium is running in headless mode.
This guide shows how to run selenium in headless mode for Chromium-based (tested on Google Chrome and Chromium) and Firefox browsers. Before we do that, however, let’s cover some prerequisites (essential).
Prerequisites
Before using the code in this article, ensure that:
#1. Ensure you are running Python 3.
#2. [IMPORTANT] Ensure you are using the latest version of selenium
You can use pip to install or upgrade selenium by running the following line
pip install -U selenium
If you want to install a specific version of Selenium, use the following command (I am running Selenium 4.8.3 in this article).
pip install selenium==4.8.3
Running Selenium in Headless Mode in Firefox
Step 1: Import the required classes and functions
First, you need to import Firefox webdriver from Selenium. This function contains the classes of different browsers. Second, import the Options() from selenium.webdriver.firefox.options.
1 2 |
<br>from selenium.webdriver import Firefox from selenium.webdriver.firefox.options import Options |
Step 2: Create options with headless property
To run Firefox in the background without the GUI, we need to set the headless property through the Options() class as follows:
1 2 |
<br>options = Options() options.add_argument("--headless") |
Step 3: Invoke Firefox in headless mode
This can now be done by passing the options attribute in the Firefox Webdriver.
1 |
driver = Firefox(options=options) |
Let’s put it all together
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# Import required functions and classes from selenium.webdriver.firefox.options import Options # "By" is used to locate elements in the website using TAG_NAME, ID, etc. from selenium.webdriver.common.by import By # Load Firefox webdriver from selenium.webdriver import Firefox # Options() instance options = Options() # Add headless property to Options() options.add_argument("--headless") # Start Firefox webdriver and pass the options driver = Firefox(options=options) # Visit a site driver.get("http://example.com/") # Use the TAG_NAME to extract text content from the site. content = driver.find_element(By.TAG_NAME, "body").text print(content) # Quit the Firefox driver driver.quit() |
Output:
Example Domain This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission. More information…
Running Selenium in Headless mode in Chrome
You can invoke Chrome in the background using the Selenium package by slightly modifying what we did above. Here are the most important lines.
1 2 3 4 5 6 7 8 9 10 11 |
from selenium.webdriver.Chrome.options import Options # Import Chrome webdriver from selenium.webdriver import Chrome # Initialize the Options() class options = Options() # Add the headless property to the options. options.add_argument("--headless") # You may need to uncomment the following line # options.add_argument("--disable-gpu") # Start Chrome webdriver. driver = Chrome(options=options) |
Then you can access a given site with the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from selenium.webdriver.Chrome.options import Options from selenium.webdriver import Chrome from selenium.webdriver.common.by import By options = Options() options.add_argument("--headless") options.add_argument("--disable-gpu") driver = Chrome(options=options) driver.get("http://example.com/") # Extract text content from "body" tag content = driver.find_element(By.TAG_NAME, "body").text print(content) # Quit the Chrome driver. driver.quit() |
Conclusion
This guide discussed running Python selenium in headless mode in Firefox and Chrome browsers. When using the code in this article, ensure that you install the latest version of Selenium (the code in this post is based on Selenium v4.8.3).