Python Selenium is a powerful tool that allows developers to automate web browsers for testing and scraping purposes. While Selenium supports various browsers, this article focuses on setting up Selenium for the Firefox browser in Python. We will explore three different methods of setting up Selenium for Firefox, catering to different preferences and requirements.
These methods include using Selenium Manager (beta feature), utilizing the webdriver_manager
module, or manually downloading the Firefox WebDriver and using its file path in our code.
Install Selenium for Firefox
Setting up “Selenium” and configuring the Webdriver for Firefox are two separate tasks. First, we will setup selenium (for those who haven’t yet), then later discuss the various methods to install the Firefox webdriver.
To install the selenium library using pip, use the following command.
1 | pip install selenium |
or for Linux/macOS systems.
1 | pip3 install selenium |
Method 1: Selenium Manager (Beta Feature)
Starting from Selenium version 4.6.0, Selenium Manager, a new feature currently in beta, simplifies the setup process. Selenium Manager automates the process of downloading the appropriate web drivers for different browsers, including Firefox.
This feature is enabled by default, and is meant to make selenium a “batteries-included” library (which it previously wasn’t). All you need to do is use normal selenium code, without any additional code (which you might have previously had)
1 2 3 4 5 | from selenium import webdriver driver = webdriver.Chrome() driver.get( "https://example.com" ) driver.quit() |
If the above code runs without any error, that means your selenium manager is working.
However, this is a beta feature (at the time of writing) and may not account for all types of browsers and may not be compatible with all versions. Sometimes you may deliberately be using an older browser version, etc.
Method 2: Using the webdriver_manager Module
The webdriver_manager
module simplifies the management of web drivers across different browsers, including Firefox. It automatically downloads and sets up the required driver for the selected browser.
To set up Selenium for Firefox using the webdriver_manager
module, follow these steps:
First, Install both Selenium and webdriver_manager
using the following pip command:
pip install webdriver_manager
Next, we need to make a few extra imports. Besides the selenium webdriver import, we will also be importing the GeckoDriverManager and the FirefoxService classes.
1 2 3 | from selenium import webdriver from webdriver_manager.firefox import GeckoDriverManager from selenium.webdriver.firefox.service import Service as FirefoxService |
We will now use these new imports in the creation of our driver object. The driver object is responsible for all data extraction and parsing related tasks.
1 2 | service = FirefoxService(executable_path = GeckoDriverManager().install()) driver = webdriver.Firefox(service = service) |
We can now fetch data from websites.
1 | driver.get( "https://example.com" ) |
Don’t forget to close the driver after you are done using quit()
.
1 | driver.quit() |
If you want to learn how to configure webdriver_manager
for other browsers, you can refer to this tutorial.
Method 3: Downloading Webdrivers for Firefox
If you prefer a manual approach, you can download the Firefox WebDriver manually and specify its file path.
To set up Selenium for Firefox manually, follow these steps:
- Download the Firefox WebDriver: Visit the official Mozilla GitHub repository (https://github.com/mozilla/geckodriver/releases) and download the appropriate version of the Firefox WebDriver for your operating system.
- Extract the WebDriver: Extract the downloaded file to obtain the executable WebDriver file.
- Import Required Libraries: In your Python script, import the necessary libraries by adding the following lines:
1 | from selenium import webdriver |
- Specify the WebDriver Path: Create an instance of the Firefox driver and specify the path to the downloaded WebDriver by adding the following lines:
1 | driver = webdriver.Firefox(executable_path = '/path/to/geckodriver' ) |
Replace /path/to/geckodriver
with the actual file path of the downloaded WebDriver.
- Begin using your newly setup selenium webdriver for Firefox!
1 | driver.get( "https://example.com" ) |
If you do not like the idea of using hard-coded file-paths in your code, and do not want to use any of the above methods (method 1 and 2), then you can add the filepath of your Firefox webdriver into PATH (environment variables). However, do not the add the path of the webdriver exe. Instead, add the file path of the FOLDER in which the Firefox Webdriver exe is located.
This marks the end of the “Python Selenium for Firefox” Tutorial. Any suggestions of contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.
and what would happen if I apply the statement above several times (run the scenario several times)? It would install driver manager every time, or it will detect it is already installed?