Main Ecosystems
HomePython CorePandas ReferenceNumPy ScientificFastAPI & PydanticDjango Enterprise
More Ecosystems
Environment & SetupRequests & HTTPAsyncIO ConcurrencyObject-Oriented OOPPyTorch Deep LearningScikit-Learn MLFlask FrameworkWeb ScrapingDatabase & ORMDevOps & Docker

Scraping/Selenium: ModuleNotFoundError: No module named selenium and WebDriver Setup

Verified FixPython 3.10+Selenium 4.18+Silo: scraping

Quick Fix / Solution Rapide

This error occurs when Python tries to execute from selenium import webdriver without installing Selenium in the active Python environment. Install Selenium via python -m pip install selenium.

Root Cause Analysis

This error occurs when Python web scraping scripts execute import selenium or from selenium import webdriver, but the active Python interpreter cannot locate the selenium package in sys.path.

Root Cause 1: Package Not Installed in Project Virtualenv

The virtual environment executing the script has not had selenium installed in its site-packages.

Root Cause 2: IDE Interpreter Disconnect

The terminal shell where pip install selenium was run used a different Python executable than the one configured in the editor (VS Code / PyCharm).

Root Cause 3: Local Script Naming Conflict (selenium.py)

A local file named selenium.py in the working directory shadows the official package.

Root Cause 4: Incomplete Migration to Selenium 4.x

Running legacy Selenium 3 code in a new virtual environment without reinstalling modern Selenium 4 dependencies.

Reproduction Code (MCVE)

Example: Bug Reproduction
import sys

# Simulating missing selenium package in active environment
def check_selenium_installation():
    if "selenium" not in sys.modules and not any("selenium" in p for p in sys.path):
        raise ModuleNotFoundError(
            "ModuleNotFoundError: No module named 'selenium'. "
            f"Active interpreter: {sys.executable}. "
            "Please install it using: python -m pip install selenium"
        )

check_selenium_installation()

Solution 1: Install Selenium 4 in the Active Python Environment

Install official selenium using python -m pip install selenium.

Example: Recommended Solution
import sys

# Solution 1: Terminal install command
cmd = f'"{sys.executable}" -m pip install selenium webdriver-manager'
print("Run command in terminal:")
print(f"  $ {cmd}")
assert "pip install selenium" in cmd

Solution 2: Selenium 4 Modern Headless Browser Setup Pattern

In Selenium 4, ChromeDriver is managed automatically by Selenium Manager without manual driver downloads.

Example: Alternative Solution
# Solution 2: Modern Selenium 4 setup snippet
# In production:
# from selenium import webdriver
# from selenium.webdriver.chrome.options import Options
# options = Options()
# options.add_argument("--headless=new")
# driver = webdriver.Chrome(options=options)

print("Selenium 4 features automatic driver management out of the box.")
assert True

In Selenium 4 (v4.6+), you no longer need chromedriver_autoinstaller or manual executable paths. webdriver.Chrome() automatically downloads and matches the correct ChromeDriver for your installed Chrome browser.

Note de reproductibilité : Cette erreur dépend de l'état de l'environnement virtuel Python et des chemins d'installation du package selenium.

Contrast ModuleNotFoundError: No module named 'selenium' with WebDriverException: The former means Python cannot find the library; the latter means the library is installed but cannot communicate with the browser binary.