How to install GeckoDriver for Selenium Python?

GeckoDriver acts as a link between your Selenium tests and Firefox. This guide explains in detail how to install GeckoDriver for Selenium Python.

Get Started free
Guide Banner Image
Home Guide How to install GeckoDriver for Selenium Python?

How to install GeckoDriver for Selenium Python?

GeckoDriver is a web browser engine that is used by a lot of applications built by the Mozilla Corporation. It is used as a proxy by the W3C WebDriver-compatible clients to interact with Gecko browsers. GeckoDriver acts as a link between your Selenium tests and Firefox. This guide explains in detail how to install GeckoDriver for Selenium Python.

What is GeckoDriver?

While testing web applications, it is often efficient to use automation test scripts in order to check for various requirements such as functionality and useability.  Selenium is an open-source automation framework that is highly utilized in the software testing industry to automate testing for web applications. It supports multiple popular languages such as: Python, Java, Ruby, PHP, JavaScript; and multiple popular OS such as Windows, Linux, Mac, Android, and iOS.  Additionally, Selenium also supports several established browsers such as Google Chrome, Mozilla Firefox, Safari, and Microsoft Edge.

Numerous applications developed by the Mozilla Foundation and Mozilla Corporation utilize the web browser engine Gecko. Most notably, Firefox, which is a world-renowned and highly used browser, is actually based on Gecko.

In order for Selenium to be able to communicate with the Firefox browser – a browser driver is required. GeckoDriver is aptly named, since it allows communication between Selenium and the Gecko-based Firefox browser.

Selenium WebDriver is a well-liked Selenium feature that enables cross platform testing as well as OS-level configuration and management of the browsers. The browser driver, GeckoDriver, is required to interface between WebDriver-enabled clients and the browser, Firefox, to execute the automation test scripts written in various programming languages.

How Does GeckoDriver Work?

To run automation scripts with Selenium on any browser, a compatible browser driver must be utilized. GeckoDriver allows the user to automate various actions on Gecko-based platforms such as Firefox.

Interestingly, GeckoDriver doesn’t directly communicate with the Firefox browser, it needs to go through Marionette. Marionette is Firefox’s automation driver and uses Firefox’s automation protocol. Essentially Marionette accepts requests sent to it via GeckoDriver and executes them in Gecko, performing functions such as automated actions on the user interface. GeckoDriver is needed as a go-between for Selenium and Marionette because Marionette protocol isn’t compatible with Selenium.

When running a test script in Selenium, the geckodriver.exe file must be used to implement the WebDriver protocol. Compatibility is the main benefit of utilizing GeckoDriver over the built-in Firefox driver. GeckoDriver and Selenium communicate using the W3C WebDriver protocol. The universally accepted Web Driver standard is W3C. This trait of GeckoDriver is highly beneficial to developers since it makes the tests more consistent across browsers and stable.

How it works scaled

The figure above illustrates how GeckoDriver works.

geckodriver.exe launches a local server that can be used to execute test scripts. By acting as a proxy,  interfacing with Selenium, and translating requests into Marionette automation protocol. GeckoDriver establishes communication between Selenium and the Firefox browser.

How to Download and Install GeckoDriver for Selenium Python

Step 1: GeckoDriver can be installed from this link here. Pick the version of GeckoDriver based on the system being utilized. In this tutorial, the system is 64-bit and runs on Windows OS.

geckodriver versionsinstallation

Step 2: Unzip the file and obtain geckodriver.exe.  This executable file needs to be accessible when running a program with Selenium, and there are three possible methods to accomplish this task.

  1.  Edit the Path variable using the Advanced system
  2. Specify the executable path of geckodriver.exe within the test script
  3.  Use the web-driver manager package

Method 1: The first method is to edit the Path variable using the Advanced system settings.

  • Go to Advanced System Settings, a system properties window will open.

system properties

  • Click on Environment Variables.
  • Go to System Variables and find the “Path” variable. Select this variable and click Edit.

environment variables

  • Click New in the Edit environment variable window. Add the location of the geckodriver.exe file to the Path. In this example, the file is located in C:\DRIVERS.

edit env variables

  • Restart the computer prior to running any test scripts.

Method 2: Specify the executable path of geckodriver.exe within the test script.

  • Simply enter the executable path of the geckodriver.exe file when initiating the driver.
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\Program Files (x86)\geckodriver.exe')

Method 3: Use the web-driver manager package.

  • Install webdriver-manager. Pip is the most efficient way to install python packages. If you have anaconda setup then simply enter the following command into the anaconda powershell window or directly into the linux terminal.
pip install webdriver-manager
  • Simply use the webdriver-manager package to obtain the appropriate driver for Firefox.
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
  • The webdriver-manager package simplifies management by allowing the user to install and use the right version of the browser driver required for their test script. See the following example using the webdriver-manager package:

WD output1

  • The package first checks if the driver is already present in the cache. If it isn’t found, then the manager will check the version of Firefox being used on the system and then install the latest driver compatible with the system and cache it. Then the next commands in the test script will be executed via the driver.

The above-mentioned methods are the easiest to utilize and work on any system, be it Windows, Linux, or macOS.

Alternatively, the following steps can also be used to download and set up GeckoDriver via the command line on any Linux/macOS system.

1. Pick the right version of GeckoDriver based on the system being utilized from the releases available. For this example, the latest version is “geckodriver-v0.32.0-linux64.tar.gz”.

2. Simply enter the following command into the terminal to install this release.

wget https://github.com/mozilla/geckodriver/releases/download/v0.32.0/geckodriver-v0.32.0-linux64.tar.gz

The following lines will be seen once the download is completed.

GeckoDriver linux

3. If you haven’t directly downloaded it into the right location, move the tar file to the appropriate folder with the following command.

mv geckodriver-v0.32.0-linux64.tar.gz softwares/

The file will be moved to the software directory.

4. Now unzip the file with the following command

tar -xvzf geckodriver-v0.32.0-linux64.tar.gz

The geckodriver.exe file will now be available.

5. Lastly, set the PATH in the .bashrc file with the following command.

export PATH=$PATH:/mnt/c/Users/Asus/softwares/geckodriver*

6. Install Conda on the Linux terminal, then use the following command to directly install GeckoDriver.

conda install -c conda-forge geckodriver

How to Launch the Firefox Browser Using GeckoDriver in Selenium Python

Pre-requisites

  1. Set up a Python environment.
  2. Install GeckoDriver and use any of the methods outlined above to ensure that the driver is accessible when running the test script.
  3. Ensure that Selenium is installed. If it isn’t, use the pip package installer to install the package. If you have Conda or Anaconda set up, simply enter the following command in the Linux terminal, or the Conda/Anaconda prompt.
pip install selenium

Steps to Launch the Firefox Browser

Step 1: Import the WebDriver and options module from Selenium.

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

Step 2: Options is a concept that was added to Selenium in order to allow the user to customize the Firefox session. In this example, it is used to provide the binary location of firefox.exe.

options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'

Step 3: Initialize the browser driver, and pass an instance of the options class in the driver initialization. If GeckoDriver hasn’t been added to the path, then execute the following command.

driver = webdriver.Firefox(executable_path=r'C:\Program Files (x86)\geckodriver.exe', options=options)

However, if the geckodriver.exe file location has been added to the path, then execute the following command.

driver = webdriver.Firefox(options=options)

Step 4: Launch the Firefox browser and navigate to a website.

driver.get('https://www.bstackdemo.com/')

Output:

WD output2

Alternatively, this process is made far easier if the WebDriver-manager package is utilized.

See the example code below:

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

driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
driver.get('https://www.bstackdemo.com/')

Output:

WD output1

WD output2

Tags
Selenium