Message: 'geckodriver' executable needs to be in PATH

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# Message: 'geckodriver' executable needs to be in PATH

To solve the Selenium error "WebDriverException: Message: 'geckodriver' executable needs to be in PATH", install and import the webdriver-manager module by running pip install webdriver-manager.

The module simplifies management of binary drivers for different browsers.

geckodriver executable needs to be in path

shell
raise WebDriverException( selenium.common.exception.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. )

# Install the webdriver-manager module

Open your terminal in your project's root directory and install the webdriver-manager module.

shell
pip install webdriver-manager pip3 install webdriver-manager # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python -m pip install webdriver-manager python3 -m pip install webdriver-manager # ๐Ÿ‘‡๏ธ For Anaconda conda install -c conda-forge webdriver-manager

After you install the webdriver-manager package, you can import it and use it as follows.

main.py
# โœ… For selenium 4 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("http://www.python.org") driver.close()

The code sample above works in selenium 4, if you use selenium 3, use the following import statement and initialization code.

main.py
# โœ… For selenium 3 from selenium import webdriver from webdriver_manager.firefox import GeckoDriverManager driver = webdriver.Firefox(executable_path=GeckoDriverManager().install()) driver.get("http://www.python.org") driver.close()

If you aren't sure which selenium version you are using, use the pip show selenium command.

shell
pip show selenium

The example shows how to use the webdriver-manager module with the Firefox browser, but it can be used with all other browsers as well.

Here is an example that uses the module with Chrome.

main.py
# โœ… For selenium version 4 from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(service=ChromeService( ChromeDriverManager().install())) driver.get("http://www.python.org") driver.close()

If you use selenium version 3, use the following import statement and initialization code.

main.py
# โœ… For selenium version 3 from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(ChromeDriverManager().install()) driver.get("http://www.python.org") driver.close()

You can view examples of how to use the webdriver-manager module with all other browsers in the official pypi page.

The webdriver_manager module provides a way to automatically manage drivers for different browsers.

When using the webdriver_manager package, you don't have to download the binary, unzip it on your PC and set the path.

By using the webdriver_manager module, we can just call the install method on the specific driver manager and it just works.

# Downloading geckodriver manually

If you decide to not use the webdriver_manager module, you have to download geckodriver manually set up the correct path when initializing a Firefox browser.

  1. First, visit the geckodriver releases page and scroll down to the Assets section.

geckodriver executable needs to be in path

  1. Notice that there are different files for the different operating systems. Some of the files are for Linux, others are for macOS and there are also zip files for Windows.

  2. Download the correct zip or gz file depending on your operating system.

  3. The selenium module tries to find the geckodriver executable from the system PATH environment variable.

    • You either have to extract the zip or gz file to a directory and then manually add the directory to the path.
    • Or you can extract the zip file in a directory that is already in your PATH, e.g. your Python directory.

You can check what directories are on your PATH and move the geckodriver.exe file to one of the directories.

To see what directories are already on your PATH, issue the following command in bash or zsh.

bash_or_zsh
echo $PATH

And the following command on Windows.

cmd
echo %PATH%

If the geckodriver.exe file is not in a directory that's already on PATH, add the directory to your PATH with the following command.

For bash shell:

bash
echo 'export PATH=$PATH:/path/to/driver' >> ~/.bash_profile source ~/.bash_profile

For zsh shell:

zsh
echo 'export PATH=$PATH:/path/to/driver' >> ~/.zshenv source ~/.zshenv

For Windows:

cmd
setx PATH "%PATH%;C:\WebDriver\bin"

You can test if the driver has been added correctly by issuing the following command.

For bash or zsh:

shell
geckodriver

For Windows:

cmd
geckodriver.exe

If you aren't able to start the Firefox browser without specifying a path, try explicitly setting the path to the geckodriver.exe file.

main.py
# โœ… For selenium 4 from selenium import webdriver from selenium.webdriver.firefox.service import Service as FirefoxService driver = webdriver.Firefox(service=FirefoxService( r'C:/Users/bobbyhadz/Documents/geckodriver.exe') ) driver.get('https://python.org')

Make sure to update the path to the geckodriver.exe file in the code sample.

You can also follow the official selenium guide to set up geckodriver.

# Starting with version 4.6, Selenium comes with batteries included

In November 2022, the selenium team introduced Selenium version 4.6.

The version comes with the batteries included and only requires you to have Firefox installed.

The Selenium package now configures the driver for Firefox if it isn't present on the PATH environment variable.

If your selenium version is older than 4.6, update the package by running the following command.

shell
pip install selenium --upgrade pip3 install selenium --upgrade # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python -m pip install selenium --upgrade python3 -m pip install selenium --upgrade

You can use the pip show command to check your selenium version.

shell
pip show selenium

If you already have the browser driver installed on your machine, the feature of automatically setting up browser drivers will be ignored.

The feature is only in effect if selenium cannot find the browser drivers present on your PATH environment variable.

I've also written an article on Message: 'chromedriver' executable needs to be in PATH.

The error occurs when using Chrome with Selenium.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev