After installing Selenium and attempting to import webdriver from it, I see the following error:
selenium.common.exceptions. WebDriverException: Message: Python needs 'geckodriver' executable to be in PATH.
Therefore, let’s begin with this article and learn about all potential methods that can work to solve the error.
Why selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH Error Occurs?
This selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH Error occurs, because the Selenium client bindings search the system PATH for the geckodriver executable but it doesn’t able to find it.
The error indicates that Firefox was installed at a place different than the default while Selenium attempted to locate Firefox and start it from the default location but was unable. You must explicitly provide the path of the Firefox installed binary location to launch Firefox.
How to Solve selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH Error?
To Solve the Error, the directory containing the executable must be added to the system path. The Selenium client bindings search the system PATH for the geckodriver executable.
Below are the all possible ways I tried to solve the selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH Error:
Fix 1: Add Firefox executable_path for Exact Location
If you are using a Bash-compatible shell on a Unix-based system, you may add it to the system’s search path as follows:
export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('path/to/installed firefox binary')
browser = webdriver.Firefox(firefox_binary=binary)
On Windows, the Path system variable must be changed manually or through the command line in order to add the whole directory path to the executable geckodriver.
To Solve issue in windows, Download the file from Github, extract it and paste the file to python and it should solve the error.
C:\Users\MYUSERNAME\AppData\Local\Programs\Python\Python39
Fix 2: Use webdriver-manager
Another way to solve the issue is to use the webdriver-manager and let it set everything for you. now you don’t require to do all manually, it would setup automatically.
Step 1: Install webdriver-manager
pip install webdriver-manager
Step 2: Import GeckoDriverManager using the below command
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
Step 3: Install GeckoDriverManager using below command
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
So overall commands that you need to use to solve the error is below:
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
driver.get('https://google.com')
Now you can use the Firefox without manually download and importing. It should solve yours error.
Conclusion
selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH error indicates that Firefox was installed at a place different than the default while Selenium attempted to locate Firefox and start it from the default location but was unable.
You must explicitly provide the path of the Firefox installed binary location to launch Firefox.
To Solve the selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH Error, either Add Firefox executable_path for Exact Location OR Use webdriver-manager.