How to Solve ChromeDriver Errors in Selenium: "needs to be in PATH" and "session not created"
This guide addresses two common errors when using Selenium with ChromeDriver:
WebDriverException: Message: 'chromedriver' executable needs to be in PATH
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version X
You'll learn the causes of these errors and the best solutions, primarily using the webdriver-manager
package. We'll also briefly cover manual ChromeDriver installation and Selenium's built-in driver management (version 4.6+).
The Easy Solution: webdriver-manager
(Recommended)
The webdriver-manager
package automatically downloads and manages the correct ChromeDriver version for your installed Chrome browser. This is the recommended solution because it avoids manual downloads, PATH issues, and version mismatches.
Install:
pip install webdriver-manager selenium
- Make sure to install
selenium
as well.
Selenium 4
If you're using Selenium 4 (the current version), the code is very simple:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
import time
service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
driver.get("http://www.python.org")
time.sleep(2)
driver.quit() #Always quit the driver.
ChromeDriverManager().install()
: This downloads the correct ChromeDriver version (if needed) and returns the path to the executable.Service
: TheService
class manages the starting and stopping of ChromeDriver. You pass the path to the ChromeDriver executable to theService
constructor.webdriver.Chrome(service=service)
: This creates a new Chrome instance, using the specified service.
Selenium 3 (if you must use it)
If you're using an older version of Selenium (version 3), the code is slightly different:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("http://www.python.org")
time.sleep(2)
driver.quit() #Always quit the driver.
- You directly pass the result of
ChromeDriverManager().install()
to thewebdriver.Chrome()
constructor. NoService
object is needed in Selenium 3. But you should upgrade to Selenium 4!
Using Chromium
If you're using Chromium (the open-source project behind Chrome) instead of Google Chrome, use ChromeType.CHROMIUM
:
# 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
import time
driver = webdriver.Chrome(service=ChromiumService(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()))
driver.get("http://www.python.org")
time.sleep(2)
driver.quit()
# Selenium 3 (older) - avoid if possible
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType
import time
driver = webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())
driver.get("http://www.python.org")
time.sleep(2)
driver.quit()
Manual ChromeDriver Installation (Less Recommended)
While webdriver-manager
is strongly recommended, here's how to install ChromeDriver manually if you must:
-
Download: Go to the official ChromeDriver downloads page (https://chromedriver.chromium.org/downloads) and download the version that matches your Chrome browser version. This is crucial.
-
Extract: Unzip the downloaded file.
-
Place in PATH (Option 1): Move the
chromedriver
executable (orchromedriver.exe
on Windows) to a directory that's already on your system'sPATH
. Common locations include:/usr/local/bin
(Linux/macOS - often requiressudo
)/usr/bin
(Linux - often requiressudo
)- A directory already on your Windows
PATH
(you can check this by runningecho %PATH%
in the command prompt)
-
Add to PATH (Option 2): Alternatively, you can add the directory containing
chromedriver
to your system'sPATH
environment variable. The process for this varies depending on your operating system (search online for "add directory to PATH" + your OS). This is generally less recommended than placing the executable in a standard location. -
Specify Path in Code (Option 3): If you don't put ChromeDriver in your PATH, you must provide the full path to the executable in your code:
# Selenium 4 example, providing the path
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
chromedriver_path = r'C:/Users/tutorialreference/Documents/chromedriver.exe' # Replace with YOUR path
service = Service(executable_path=chromedriver_path) # Pass the path to Service.
driver = webdriver.Chrome(service=service)
driver.get("http://www.python.org")
time.sleep(2)
driver.quit()# Selenium 3 example, providing the path
import time
from selenium import webdriver
chromedriver_path = r'C:/Users/tutorialreference/Documents/chromedriver.exe' # Replace with YOUR path
driver = webdriver.Chrome(executable_path=chromedriver_path) # Pass path directly
driver.get("http://www.python.org")
time.sleep(2)
driver.quit()
Selenium 4.6+ Built-in Driver Management
Starting with Selenium 4.6.0, Selenium has built-in support for managing ChromeDriver (and other drivers). If the driver is not found in PATH
, Selenium will try to download it. This simplifies setup:
from selenium import webdriver
import time
# Selenium 4.6+ will attempt to download ChromeDriver if needed.
driver = webdriver.Chrome()
driver.get("http://www.python.org")
time.sleep(2)
driver.quit()
- With Selenium 4.6+, you often don't need
webdriver-manager
or manual installation. However,webdriver-manager
is still useful for managing specific driver versions or working with older Selenium versions.
Understanding the "session not created" Error
The selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version X
error occurs when there's a version mismatch between your installed Chrome browser and the ChromeDriver executable.
- Cause: ChromeDriver is very tightly coupled to the Chrome version. You must use a ChromeDriver version that matches your Chrome version.
- Solution:
webdriver-manager
solves this problem automatically. If you're installing manually, you must download the correct version from the ChromeDriver website.
Conclusion
This guide explained how to resolve common ChromeDriver-related errors in Selenium.
- The
webdriver-manager
package is the recommended solution for most users, as it automatically handles driver downloads and version compatibility. - Selenium 4.6+ has simplified this further with built-in driver management.
- Manual installation is possible but requires careful attention to version matching and PATH configuration.
- Always prefer the automated solutions (
webdriver-manager
or Selenium 4.6+) unless you have a very specific reason not to. - Always remember to close the driver with
driver.quit()
.