ModuleNotFoundError: No module named 'selenium'
Posted in Python by Dirk - last update: Feb 05, 2024
Python raises the ModuleNotFoundError: No module named 'selenium
when it is unable to find the Selenium
library. The most likely cause is that you didn’t install Selenium in the environment where you are running your code. Quick fix: install Selenium using: the pip install selenium
command
Possible causes and solutions
Selenium is an open-source framework designed to automate web browsers. It provides a way for developers and testers to write scripts in various programming languages (including Python, Java, C#, and others) to automate interactions with web browsers. Selenium is often used for web testing, automating repetitive tasks in a browser, and web scraping.
Sample code:
from selenium import webdriver
# Set the path to the webdriver executable (e.g., chromedriver.exe)
driver_path = '/path/to/chromedriver'
# Create a new instance of the Chrome WebDriver
driver = webdriver.Chrome(executable_path=driver_path)
# Open a website
driver.get('https://www.example.com')
# Find an element by its ID and interact with it
element = driver.find_element('id', 'some_element_id')
element.click()
# Close the browser
driver.quit()
And when trying to run the code you get the ’no module named error':
Traceback (most recent call last):
File "myfile.py", line 1, in <module>
ModuleNotFoundError: No module named 'selenium'
Luckily, it is easy to fix. Below you can find the most common causes and how to fix them:
‘selenium’ Library is not installed
In your code you are importing the webdriver module from selenium
from selenium import webdriver
which generates the ModuleNotFoundError: No module named 'selenium'
. The the most common cause for this error: the environment where you are running your code doesn’t have the selenium
library installed.
You can use the following command in the terminal or command prompt to check if the selenium
library is installed:
If selenium
is not installed you will get this result:
WARNING: Package(s) not found: selenium
Solution:
‘selenium’ Module installed in the Wrong Directory
It is possible that you did install the Selenium module, but that the directory where you installed it is not in the Python path.
You can check the Python path in a few different ways:
1. Using sys module in Python:
Open a Python script or the Python interpreter and run the following code:
import sys
print(sys.path)
This will print a list of directories that make up the Python path.
2. Using Command Line
Open a command prompt or terminal and run:
If the PYTHONPATH
environment variable is set, it will display the directories in the Python path.
Solution: Move the selenium module to a directory that is included in the Python path or add the Selenium module’s directory to the sys.path
using sys.path.append()
.
‘selenium’ not installed in Virtual Environment
Even if you have Selenium installed in on your computer, if you are using a virtual environment you cannot access it from this environment.
If you are using virtual environments, ensure that the virtual environment is activated, and that the selenium modules is installed within this environment.
Installation is simple:
File Naming Conflicts:
It is not really likely - but if you happen to have a selenium.py
file in your environment and you also installed the standard selenium library it will cause a conflict. Rename your script/module to avoid conflicts with existing module names.
Other articles