Last updated: Apr 8, 2024
Reading timeยท4 min
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.
raise WebDriverException( selenium.common.exception.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. )
Open your terminal in your project's root directory and install the
webdriver-manager
module.
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.
# โ 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.
# โ 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.
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
.
# โ 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.
# โ 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.
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.
geckodriver
manuallyIf 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.
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.
Download the correct zip
or gz
file depending on your operating system.
The selenium module tries to find the geckodriver
executable from the
system PATH
environment variable.
zip
or gz
file to a directory and then
manually add the directory to the path.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
.
echo $PATH
And the following command on Windows.
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:
echo 'export PATH=$PATH:/path/to/driver' >> ~/.bash_profile source ~/.bash_profile
For zsh
shell:
echo 'export PATH=$PATH:/path/to/driver' >> ~/.zshenv source ~/.zshenv
For Windows:
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
:
geckodriver
For Windows:
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.
# โ 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
.
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.
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.
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.