Running Headless Selenium in Python (2023)

In the world of web testing and automation, Selenium has become a popular tool for developers and testers alike. It allows us to simulate user interactions with web applications, automate repetitive tasks, and perform end-to-end testing. But have you ever wondered if there’s a way to run Selenium tests without a visible browser window? That’s where headless Selenium in Python comes into play.

In this article, we’ll explore the concept of running Selenium in a headless mode and its benefits. We’ll focus on using headless Selenium in Python, assuming that you already have a basic understanding of Selenium and have it installed. So, let’s dive in and uncover the wonders of headless Selenium testing!


Setting Up the Environment

Before we embark on our headless Selenium journey, let’s ensure our environment is properly set up. Here are the prerequisites you’ll need:

1. Python: Ensure that you have Python installed on your system. You can download the latest version from the official Python website.

2. Selenium: Install the Selenium library using pip, the Python package manager. Open your terminal or command prompt and run the following command:

pip install selenium

3. WebDriver: Selenium requires a WebDriver to interact with web browsers. The WebDriver acts as a bridge between Selenium and the browser. Depending on the browser you wish to use, you’ll need to download the appropriate WebDriver. You can read this introductory tutorial on selenium for instructions on how to set this up if you haven’t already.

Once you have Python, Selenium, and the WebDriver installed, we’re ready to configure headless options for our Selenium tests.


Changes in Selenium Headless Mode Configuration

In the world of Selenium, running automated scripts in headless mode has been a convenient way to execute tests without a visible browser window. However, there have been recent changes in the configuration of headless mode in Selenium 4.8.0 (Jan. 2023) that users need to be aware of.

Previously, Selenium provided a convenience method to set the headless mode while configuring browser options. This method allowed users to easily enable headless mode with a simple boolean parameter. However, starting from Selenium 4.8.0, this convenience method will be deprecated, and users will need to set the headless mode through arguments when configuring the browser options.

So, why is Selenium making this change?

It turns out that Chromium-based browsers now have two different headless modes available. The original headless mode, and a newer mode with enhanced capabilities introduced in 2022 (version 109). When users used the convenience method in Selenium to enable headless mode, it would utilize the original method provided by Chromium-based browsers.

This left users with no way of accessing the newer headless mode offered by Chrome, since the only options were to either “enable” or “disable” headless mode.

By deprecating and eventually removing the convenience method (scheduled for Selenium 4.10.0), Selenium is giving users full control to choose which headless mode they want to use. This allows users to leverage the newer headless mode, which offers enhanced browser functionality and the ability to run extensions.

We will be including both the old and new ways of configuring headless mode in the next section.


Headless Mode before Selenium 4.8.0

Here is how to configure headless mode in both the Chrome and Firefox browsers before selenium 4.8.0.

Chrome

from selenium import webdriver

options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(options=options)

driver.get('https://quotes.toscrape.com/')
driver.quit()

If you have configured Selenium using the webdriver manager module (like we have), your code should look like this:

from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium import webdriver

service = ChromeService(executable_path=ChromeDriverManager().install())

options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(options=options, service=service)

driver.get('https://quotes.toscrape.com/')
driver.quit()

Firefox

from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium import webdriver

service = FirefoxService(executable_path=GeckoDriverManager().install())

options = webdriver.FirefoxOptions()
options.headless = True
driver = webdriver.Firefox(options=options, service=service)

driver.get('https://quotes.toscrape.com/')
driver.quit()

Headless Mode from Selenium 4.8.0 onwards

Here is how to configure selenium headless mode in both the Chrome and Firefox browsers from selenium 4.8.0 onwards in Python.

Chrome

from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium import webdriver

service = ChromeService(executable_path=ChromeDriverManager().install())

options = webdriver.ChromeOptions()
options.add_argument('--headless=new')
driver = webdriver.Chrome(options=options, service=service)

driver.get('https://quotes.toscrape.com/')
driver.quit()

If you want to use the old Chrome version (prior to 109) you can use '--headless'.


Firefox

service = FirefoxService(executable_path=GeckoDriverManager().install())

options = webdriver.FirefoxOptions()
options.add_argument('--headless')
driver = webdriver.Firefox(options=options, service=service)

driver.get('https://quotes.toscrape.com/')
driver.quit()

This marks the end of the “Running Headless Selenium in Python” Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments