How to Solve WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH?

Did you get the “‘geckodriver’ executable needs to be in PATH?” error message when running your Python program?

Are you searching for the answers in Q& A programming websites? But are you confused about the solutions provided on their websites? Are their solutions not working to rectify the issues?

If your answer is YES. You have come to the right place where we can provide you with simple solutions to sort out this issue.

In this short article, we will explain to you four easy methods to remove the exception message. Finally, we will give our recommended method for you to resolve the issue effectively.

Problem Formulation

When you run the below code to Firefox browser using the Selenium webdriver library:

from selenium import webdriver
browser = webdriver.Firefox()

You will get following long list of Traceback as shown below:

Traceback (most recent call last):
  File "/Users/mohamedthoufeeq/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/common/service.py", line 74, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 947, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 1819, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/mohamedthoufeeq/PycharmProjects/pythonProject/main.py", line 2, in <module>
    browser = webdriver.Firefox()
  File "/Users/mohamedthoufeeq/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/firefox/webdriver.py", line 175, in __init__
    self.service.start()
  File "/Users/mohamedthoufeeq/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/common/service.py", line 84, in start
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

From the traceback, we can see two exceptions are raised. These are explained below:

  1. File Not Found Error: The error states FileNotFoundError: [Errno 2] No such. file or directory: 'geckodriver'. This means GeckoDriver is not installed in the system. Or if you have installed it, but it is not in the current path. 
  2. WebDriver Exception: The message states that the 'geckodriver' executable needs to be in PATHPATH is a variable that specifies the location of executable programs. This exception tells us that there is no instruction on where the driver GeckoDriver is located. And also, even if you specify it. The path of GeckoDriver could be incorrect.

Let us see how you can resolve the issues by using either of four solutions.

Solution 1: Manual Set-Up of GeckoDriver

This method requires the user’s manual intervention on setting up of GeckoDriver which is simple.

Step 1: Download GeckoDriver

The first step is to download the driver by using the link: geckodriver.

Step 2: Install GeckoDriver

The Second Step is to install the driver. You can unzip the driver file into your Mac to start the installation process. And then move the driver to your desired location. Make sure you have copied the Path of the driver.

Step 3: Mention Executable Path

from selenium import webdriver
# Code 2
browser = webdriver.Firefox(executable_path="/usr/local/Cellar/geckodriver/0.30.0/bin/geckodriver") 
browser.get("https://app.finxter.com/")

In the above script after importing necessary libraries such as Selenium webdriver. In the Firefox class, mention the path of GeckoDriver as shown in Code 2. In my case, it is /usr/local/Cellar/geckodriver/0.30.0/bin/geckodriver.

This path is stored in executable_path.

The executable path means the full pathname of the user’s executable file.  Now you can use a driver and get the method to access the website https://app.finxter.com/.

When you run the module. The GeckoDriver Driver stored in the driver object communicates with Firefox Browser. This makes the browser opens automatically. You can add code for web scrapping of your required details and store it in CSV file.

As mentioned earlier in this method requires user’s manual setup of Driver which is tedious. There is a method that fully automates the driver setup. Let us see Solution 2.

Solution 2: Automate Set-Up of GeckoDriver

Another way to sort out this error is by using Web Driver Manager.

Web Driver Manager is an open-source Java library used to automate the management of selenium web drivers such as GeckoDriver, ChromeDriver, and more. They manage by downloading, setup and maintenance of drivers.

In the previous solution, you have to do three manual steps first download the GeckoDriver then install it into Mac, and then set the path. In this method, only 2 steps are required to set up the driver and are fully automated.

Interesting Right!!

Let us see how you can use this method to resolve path issues.

Step 1: Install webdriver-manager Library.

Firstly, install the library using the below command in terminal

pip install webdriver-manager

Step 2: Import GeckoDriverManager

from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager # Code 2
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install()) # Code 3

In this step, you have to import Gecko Driver Manager from the web driver Manager library. Next use GeckoDriverManager().install() class for setting up the driver. Once the driver is installed into your system, The path is stored in the executable_path. you are not required to specify the PATH manually in the script. Refer to Code 2 & Code 3.

Each time when the program runs. It automates downloading of GeckoDriver from the source, then installs the driver and specifies the path to  executable_path. This method completely automates the process. It is very advantageous whenever new versions of the driver are released. The new version of GeckoDriver is released every year. On 16th September 2021 new version 0.30.0 is released.

Thus, in solution 1, you have to download the driver each time when new versions are created.

But in this method, you have to code it once and it automatically installs the latest versions of the driver.

Solution 3: Set up GeckoDriver using Homebrew

In this method, we can install GeckoDriver using Homebrew.

Homebrew installs the software you require that the MacOS system can’t.

It installs the packages into their directory and then links the files to /usr/local.

Let us study each step to set up GeckoDriver:

Step 1: install Homebrew

First, open the terminal and then paste the below command to install Homebrew:

"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Step 2: Install GeckoDriver

After installation of Home Brew is successful.

You can install Geckodriver by entering the below command:

brew install geckodriver

This commands first downloads the driver and then a zip file is extracted for installation

The Path of Geckodriver is shown as below after installation in terminal

usr/local/Cellar/geckodriver/0.30.0

You have to find out the path for the GeckoDriver exe file by following below procedure:

In Finder, click GO then GO TO FOLDER.

You have to paste the path (“usr/local/Cellar/geckodriver/0.30.0”) and enter.

You can find folder 0.30.0. Open the folders until you find the Geckodriver exe file.

Open the driver and copy the path in the terminal.

My path is  /usr/local/Cellar/geckodriver/0.30.0/bin/geckodriver.

Step 3: Mention Executable Path

Like the Solution 1 method, place the path into executable_path as shown below code:

browser = webdriver.Firefox(executable_path="/usr/local/Cellar/geckodriver/0.30.0/bin/geckodriver")

Comparing this method with Solution 1. This method is faster way to set up because as in solution 1 you are not required to go GeckoDriver link to download and then unzip the file manually.

But the Method mentioned here is best as the setup process is done automatically with a few lines of Python code.

Solution 4: Set Up GeckoDriver using webdriverdownloader

The last solution you can resolve the issue is by using the webdriverdownloader module.

This module helps to download web driver binaries such as GeckoDriver, ChromeDriver, etc.

The class of the module such as GeckoDriverdownloder is used to download and install the latest version of the driver. It will be installed in the system path /usr/local/bin.

Let us go through the following steps:

Step 1: Install the webdriverdownloader module.

Type the following command in the terminal to install it.

pip install webdriverdownloader

Step 2: Import GeckoDriverdownloder

Type the below command to import GeckoDriverDownloader

from webdriverdownloader import GeckoDriverDownloader

Step 3: Use Download and Install method

gdd = GeckoDriverDownloader()
gdd.download_and_install()

The above code will search for the latest driver version to download and install into the system path. You can search for GeckoDriver in the path /usr/local/bin.

Then use this path in executable_path for automating the browser.

This method is somewhat similar to the above solutions (2&3). Still, this method is much effective than Solution 1.

Summary

We all understand that Driver is essential to communicate with the browser. The driver interacts with the browser to open the website and to fill the forms, for example, login or password, contact form. Also, to access specific web pages and extract data. If the system can’t find the driver, we can’t automate browsers. Thus, specifying the GeckoDriver driver’s correct Path is so critical. The four solutions to rectify exception message are by manual & automated setup of Geckwebdriver, using webdriver manager and webdriver downloader. I recommend Solution 2 i.e., Set up GeckoDriver using WebDriver Manager is the best method to resolve this exception issue. This method automates the whole process from downloading to the installation of the driver.

You can use any of the above methods as per your preference besides my recommendation. Can you provide me with your valuable feedback after trying these solutions?

Did it work for you?

Programmer Humor

There are only 10 kinds of people in this world: those who know binary and those who don’t.
👩🧔‍♂️
~~~

There are 10 types of people in the world. Those who understand trinary, those who don’t, and those who mistake it for binary.

👩🧔‍♂️👱‍♀️