WebDriverException: Message: Geckodriver Executable Needs to Be in PATH Error in Python

Manav Narula Oct 10, 2023
  1. Use the geckodriver.exe File and Add It to the System PATH
  2. Use the executable_path Parameter in the webdriver.Firefox() Function
  3. Use the webdriver-manager Module
WebDriverException: Message: Geckodriver Executable Needs to Be in PATH Error in Python

The selenium package in Python can automate tasks on a web browser. Using their web drivers, we can use different web browsers like Google Chrome, Firefox, and more.

This tutorial will discuss the Message: 'geckodriver' executable needs to be in PATH error in Python.

The geckodriver is a Mozilla-developed browser engine that acts as a link between Selenium and the Firefox browser. This error occurs when the driver is not installed properly, or its path is not specified appropriately.

See the code below.

from selenium import webdriver

browser = webdriver.Firefox()

Output:

WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

Let us now discuss different ways to solve this error.

Use the geckodriver.exe File and Add It to the System PATH

Selenium tries to identify the driver executable from the system environment variable PATH. We can add the executable path of the geckodriver to this variable.

First, we must download the driver’s executable from the official Mozilla website. We need to add the path of the directory that contains this executable to the PATH variable discussed previously.

The PATH variable can be found under the Environment Variables menu. We need to right-click the This PC icon, go to Properties, and select the Advance Settings option to get this menu.

Linux users can copy the executable file directly to the /usr/local/bin directory.

Use the executable_path Parameter in the webdriver.Firefox() Function

We use the webdriver.Firefox() constructor to create the Driver object that can open the browser window and perform the automated tasks. We can specify the path of the geckodriver executable within this function using the executable_path parameter.

For example:

from selenium import webdriver

driver = webdriver.Firefox(executable_path=r"user\pathofdriver\geckodriver.exe")

Mac OS users can also install the geckodriver using homebrew. The following command can be utilized.

brew install geckodriver

After installation, the path of the driver is shown. We can copy this path, paste it into the Finder application, and click Go to Folder.

This will return the full path of the driver that can be used in the executable_path parameter.

Use the webdriver-manager Module

The webdriver-manager module was introduced to provide some relief in managing the web drivers of different browsers.

We can use the GeckoDriverManager().install() functions to install and use the executable for the geckodriver. This needs to be specified in the executable_path parameter discussed previously.

See the code below.

from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager

driver_object = webdriver.Firefox(executable_path=GeckoDriverManager().install())
Author: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn

Related Article - Python Selenium

Related Article - Python Error