How to hide Firefox window (Selenium WebDriver)?
Last Updated :
21 Aug, 2024
In situations where you might wish to execute tests without a visible browser window, while automating web tests with Selenium WebDriver; it is possible for one to run headless browsers for example which will help save resources and speed up the execution of these tests. This article will cover how to run Firefox in headless mode using Selenium WebDriver.
Prerequisites: Ensure Python is installed on your system.
Step-by-step Implementation
Step 1. Install Selenium
For this tutorial, we will need to import a selenium package.
Run the following command in your command prompt to install selenium:
pip install selenium
Step 2. Import Necessary Modules
After installing Selenium from Step 1, you need to import the required classes from the Selenium package:
Python
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
Step 3. Configure Firefox Options
Next, configure Firefox to run in headless mode.
This can be done by setting the headless option to `True`:
Python
options = Options()
options.headless = True # Enable headless mode
Step 4. Initiate web driver actions
In this step we will initiate the WebDriver with the configured headless option and perform any web actions as usual.
For example, let's open a webpage and print its title:
Python
driver = webdriver.Firefox(options=options) #Initiate web driver
driver.get("https://www.example.com")
print("Page title is:", driver.title)
Step 5. Close the Browser
Since, our actions are complete now, we have to close the browser:
Python
Complete code:
For your reference here is the full code implementation:
Python
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
# Set up Firefox options
options = Options()
options.headless = True # Enable headless mode
# Initialize the WebDriver with headless option
driver = webdriver.Firefox(options=options)
# Open a webpage
driver.get("https://www.example.com")
# Perform some actions (for example, get the page title)
print("Page title is:", driver.title)
# Close the browser
driver.quit()
Output:
Running the script above will result in the following console output without displaying any Firefox window:
Console outputConclusion
Using Selenium WebDriver to run Firefox in a headless manner is an efficient method for carrying out tests without the need for a visible browser window. This method is especially useful in non-GUI environments and can help make automated testing processes smoother.
Similar Reads
How to set Proxy in Firefox using Selenium WebDriver? In todayâs fast-paced digital world, ensuring that web applications are tested under various network conditions is crucial. Setting up a proxy in Firefox using Selenium WebDriver allows testers to capture traffic, simulate different network environments, and comply with strict corporate policies, ma
4 min read
How to set browser width and height in Selenium WebDriver? Using Selenium WebDriver, we can set the browser width and height. Various methods are available to set browser width and height. When we run any test, the browser will open in default size. So, if we need to change the size of the browser, we can do it using selenium WebDriver keywords. A few of th
2 min read
How to click on hidden element in Selenium WebDriver? In Selenium WebDriver, interacting with web elements is key to automating web applications. However, certain elements on a webpage might be hidden or obscured, making it challenging to interact with them directly. In such cases, developers and testers often need to use alternative methods, such as J
3 min read
How to check if an element exists with Selenium WebDriver? Selenium is one of the most popular tools for automating web applications. Selenium is not a single tool but rather a set of tools that helps QA testers and developers to automate web applications. It is widely used to automate user interactions on a web page like filling out web forms, clicking on
7 min read
How to open browser window in incognito/private mode using selenium webdriver? Selenium WebDriver is one of the most used tools for browser automation for the purpose of testing web applications. This makes it possible to automate a browser through the use of a simple API via different programming languages such as Java, C#, Python, and so on.While testing, sometimes you may w
3 min read