Last updated: Apr 8, 2024
Reading timeยท5 min
To solve the Selenium error "WebDriverException: Message: 'chromedriver'
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.
webdriver-manager
moduleOpen your terminal in your project's root directory and install the
webdriver-manager
module.
# ๐๏ธ In a virtual environment or using Python 2 pip install webdriver-manager # ๐๏ธ For python 3 (could also be pip3.10 depending on your version) pip3 install webdriver-manager # ๐๏ธ If you get a permissions error sudo pip3 install webdriver-manager # ๐๏ธ If you don't have pip in your PATH environment variable python -m pip install webdriver-manager # ๐๏ธ For python 3 (could also be pip3.10 depending on your version) 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 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()
The code sample above should be used for selenium version 4.
If you use selenium version 3, use the following code sample instead.
# โ 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()
If you need to check your selenium version, issue the pip show selenium
command.
pip show selenium pip3 show selenium
The example shows how to use the webdriver-manager
module with the Chrome
browser, but it can be used with all other browsers as well.
Here is an example that uses the module with Chromium
.
# โ for selenium 4 from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromiumService from webdriver_manager.chrome import ChromeDriverManager from webdriver_manager.core.utils import ChromeType driver = webdriver.Chrome(service=ChromiumService( ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())) driver.get("http://www.python.org") driver.close()
And here is an example that runs the Chromium browser for selenium
version 3.
from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager from webdriver_manager.core.utils import ChromeType driver = webdriver.Chrome(ChromeDriverManager( chrome_type=ChromeType.CHROMIUM).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.If you decide to not use the module, you have to download the binary for the drivers, unzip them on your PC and set the path to the driver when using the class from the selenium module.
By using the webdriver_manager
module, we can just call the install
method
on the specific driver manager and it just works.
If you want to download ChromeDriver manually instead of using
webdriver_manager
, follow the instructions:
zip
file for
your operating system. There are files for Linux, macOS and Windows.chromedriver.exe
file in a directory that is
already listed in your PATH, or you can place the driver in a directory and
add the directory to your PATH.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 chromedriver.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 you have added chromedriver
correctly to your PATH by issuing
the following command.
For bash
or zsh
:
chromedriver
For Windows:
chromedriver.exe
If you aren't able to start the Chrome browser without specifying a path, try
explicitly setting the path to the chromedriver.exe
file.
import time from selenium import webdriver from selenium.webdriver.chrome.service import Service driver = webdriver.Chrome(service=Service( r'C:/Users/bobbyhadz/Documents/chromedriver.exe')) driver.get("http://www.python.org") time.sleep(2) driver.close()
Make sure to specify the correct path to your chromedriver.exe
executable.
You can also follow the
official selenium guide
to set up chromedriver
.
In November 2022, the selenium
team introduced Selenium version 4.6.
The version comes with the batteries included and only requires you to have Chrome installed.
The Selenium package now configures the driver for Chrome 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.
The error "Message: session not created: This version of ChromeDriver only supports Chrome version 102" occurs when your version of ChromeDriver doesn't support your installed version of Chrome.
To solve the error install and use the webdriver-manager
package which
automatically uses the correct ChromeDriver version.
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 102 Current browser version is 100.0.4896.127 with binary path C:\Program Files\Google\Chrome\Application\chrome.exe
Open your terminal in your project's root directory and run the following command.
pip install webdriver-manager selenium # ๐๏ธ For Python 3 pip3 install webdriver-manager selenium # ๐๏ธ If you get a permissions error sudo pip3 install webdriver-manager selenium # ๐๏ธ If you don't have pip in your PATH environment variable python -m pip install webdriver-manager selenium # ๐๏ธ For python 3 python3 -m pip install webdriver-manager selenium # ๐๏ธ Using py alias py -m pip install webdriver-manager selenium # ๐๏ธ Alternative if you get a permissions error pip install webdriver-manager selenium --user
The command installs the webdriver-manager package which simplifies the management of binary drivers for different browsers.
Here is an example of using the Webdriver manager with Selenium 4.
import time from selenium import webdriver from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) driver.get("http://www.python.org") time.sleep(2) driver.close()
And here is an example of how to use the Webdriver manager with Selenium 3.
from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(ChromeDriverManager().install())
You can check how to use the Webdriver manager package with Selenium v3 and v4 for all browser types in this section of the package's pypi page.
Alternatively, you can use the chromedriver-autoinstaller package which automatically downloads and installs ChromeDriver that supports the currently installed version of Chrome.
Open your terminal in your project's root directory and run the following command.
pip install chromedriver-autoinstaller # ๐๏ธ For Python 3 pip3 install chromedriver-autoinstaller # ๐๏ธ If you get a permissions error sudo pip3 install chromedriver-autoinstaller # ๐๏ธ If you don't have pip in your PATH environment variable python -m pip install chromedriver-autoinstaller # ๐๏ธ For python 3 python3 -m pip install chromedriver-autoinstaller # ๐๏ธ Using py alias py -m pip install chromedriver-autoinstaller # ๐๏ธ Alternative if you get a permissions error pip install chromedriver-autoinstaller --user
Now you only have to import chromedriver_autoinstaller
in your module.
import time import chromedriver_autoinstaller from selenium import webdriver chromedriver_autoinstaller.install() driver = webdriver.Chrome() driver.get("http://www.python.org") time.sleep(2) driver.close()
The chromedriver-autoinstaller package automatically downloads and installs ChromeDriver for your version of Chrome.
I've also written an article on Message: 'geckodriver' executable needs to be in PATH.
The error occurs when using Firefox with selenium.