Python Selenium for Firefox: Setting up your Webdriver

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.

pip install selenium

or for Linux/macOS systems.

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)

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.

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.

service = FirefoxService(executable_path=GeckoDriverManager().install())
driver = webdriver.Firefox(service=service)

We can now fetch data from websites.

driver.get("https://example.com")

Don’t forget to close the driver after you are done using quit().

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:

  1. 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.

  2. Extract the WebDriver: Extract the downloaded file to obtain the executable WebDriver file.

  3. Import Required Libraries: In your Python script, import the necessary libraries by adding the following lines:
from selenium import webdriver
  1. Specify the WebDriver Path: Create an instance of the Firefox driver and specify the path to the downloaded WebDriver by adding the following lines:
driver = webdriver.Firefox(executable_path='/path/to/geckodriver')

Replace /path/to/geckodriver with the actual file path of the downloaded WebDriver.

  1. Begin using your newly setup selenium webdriver for Firefox!
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.

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments