In this tutorial, we will learn how to use Selenium without opening a browser using Python. Selenium is a powerful tool for controlling a web browser through the program. It is functional and widely used for both testing and web scraping.
However, it can be inconvenient to have the web browser open and sometimes it might even slow down your computer. To solve this issue, we can use Selenium with a headless browser, which means running a web browser in the background without displaying it.
Step 1: Install Selenium and WebDriver
First, the Python package manager “pip” can be used to install Selenium. Open your command prompt or terminal, and type the following command:
1 |
pip install selenium |
To use Selenium with a headless browser, we need to install a WebDriver. We will be using Google Chrome in this tutorial, so you will need to download and install the Chrome WebDriver. You can find the latest version of the Chrome WebDriver here. Make sure to download the version corresponding to your installed Chrome version.
After downloading and unpacking the zip file, remember the path to the “chromedriver” executable. You’ll need it later in the script.
Step 2: Import Necessary Packages
In your Python script, import the following packages:
1 2 |
from selenium import webdriver from selenium.webdriver.chrome.options import Options |
Step 3: Setup Headless Browser Options
To run Chrome in a headless mode, you need to specify this option using the Chrome Options Object.
1 2 |
chrome_options = Options() chrome_options.add_argument("--headless") |
Step 4: Initialize WebDriver with Headless Options
Now we can initialize the WebDriver by specifying the Chrome WebDriver’s executable path and the options we’ve created.
1 2 3 4 |
driver_path = "/path/to/chromedriver" # Replace this with the actual path to the downloaded Chrome WebDriver executable driver = webdriver.Chrome(executable_path=driver_path, options=chrome_options) |
Step 5: Use Selenium Functions as Usual
Now you can use Selenium functions as you usually do, and the browser will not open or display on the screen.
1 2 |
driver.get("https://www.example.com/") print(driver.title) |
Full Code
1 2 3 4 5 6 7 8 9 10 11 |
from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument("--headless") driver_path = "/path/to/chromedriver" # Replace this with the actual path to the downloaded Chrome WebDriver executable driver = webdriver.Chrome(executable_path=driver_path, options=chrome_options) driver.get("https://www.example.com/") print(driver.title) |
Output
Example Domain
Now you have successfully learned how to use Selenium without opening a browser with Python. This allows for more efficient and less intrusive web scraping or testing of web applications. Always remember to close the WebDriver instance properly once you have finished your task by calling driver.quit()
. This ensures that the browser process is terminated and prevents any memory leaks.