I get an error while running this selenium script. Please suggest what can be done to fix this:
Script:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import re
import csv
import time
driver = webdriver.chrome("<webdriver path>")
driver.get("https://www.google.com/")
driver.find_element_by_xpath('//*[@title="Search"]')
send_keys('abc')
driver.find_element_by_xpath('//*[@class="sbico _wtf _Qtf"]').click()
time.sleep(5)
driver.find_element_by_xpath('//[@id="rso"]/div[1]/div/div[1]/div/div/h3/a')
print(var)
Error:
Traceback (most recent call last):
File «C:/Users/admin/Desktop/test2.py», line 2, in
from selenium import webdriver
ModuleNotFoundError: No module named ‘selenium’
I have installed Python 3.6 on win 7 Professional 32 bit. I have Selenium Standalone Server version 3.4.0(link)
frianH
7,2656 gold badges19 silver badges44 bronze badges
asked May 5, 2017 at 5:43
4
Try installing selenium using pip. Use the following command.
python -m pip install -U selenium
answered May 5, 2017 at 5:57
shahinshahin
3,5051 gold badge17 silver badges17 bronze badges
3
Seems like you have not run the installation command for webdriver_manager.
Use the following command:
pip install webdriver_manager
But before this, make sure you have properly installed selenium as well. If not, use the following command to install selenium:
pip install selenium
answered Jan 15, 2020 at 19:51
Remarks to virtual environments
virtualenv
If you are using a virtual environment like virtualenv.
You have to make sure that the module selenium is installed
1.) in the virtual environment and
2.) in the default settings (when the virtual environment is deactivated).
Otherwise you will get the error message:
ModuleNotFoundError: No module named ‘selenium’
Example
Install selenium in the default settings: pip install selenium
Create virtual environment (on windows): py -m virtualenv folder_env
Activate virtual environment (on windows): source folder_env/Scripts/activate
Check virtual environment settings: which python
and which pip
Install selenium: pip install selenium
Check pip list for selenium: pip list
(Optional) Exit virtual environment: deactivate folder_env
Miscellaneous
Virtualenv by Corey Schafer: https://www.youtube.com/watch?v=N5vscPTWKOk
virtualenv is not a native module, you have to install it with
pip install virtualenv
answered Mar 3, 2019 at 7:29
1
driver = webdriver.chrome(«»)
there is no such class ^^. It is named webdriver.Chrome()
answered Mar 20, 2018 at 14:54
Corey GoldbergCorey Goldberg
58.7k28 gold badges129 silver badges143 bronze badges
Okay, the Quick and Easy Solution is to Go to Your Python Version Location, Then Libs, and then Site-packages.
ex —
C:UsersAdminAppDataLocalProgramsPythonPython38Libsite-packages
Try Deleting and Reinstalling Selenium, and Try Running the Code.
answered Apr 22, 2020 at 18:12
- You can enter these commands «pip install webdriver_manager»
- Then «pip install selenium»
answered Jun 24, 2020 at 14:49
Ankit RaiAnkit Rai
2393 silver badges3 bronze badges
If Webdriver Manager is not installed, open CMD -> Type «pip install webdriver_manager» and enter.
If you found such an issue, then in Pycharm specific application:
- Find path where your Python library exists. (Like C:UserscpAppDataLocalProgramsPythonPython38-32Libsite-packages)
- Copy site-packages folder.
- Go to Project.
- Find «Lib» folder.
- Expand Lib folder and you will find site_packages over there.
- Paste site-packages.
I believe it will help you out.
answered Aug 8, 2020 at 12:19
1
Late answer but it is worth mentioning.
Working on VSCode
under Windows
and I had this issue ModuleNotFoundError: No module named 'selenium'
, none of the solutions worked for me.
Finally, I figured out that Python
was installed twice:
-
From the Windows store which will reside under :
C:Users<user>AppDataLocalMicrosoftPython
-
Normal Install (i.e. download and install in a chosen directory, mine was
C:Users<user>AppDataLocalProgramsPython
)
What drove me crazy was when I open the terminal and run :
pip list
I found the selenium there, and it keeps telling me on the other hand : No module named 'selenium'
And it appears that VsCode
launcher (button to execute my .py file) was bound, somehow, to the former location (the one downloaded from the store) while its terminal was linked to the latter one, so I uninstall it (the one from Windows Store) and selected the proper location of Python in the launcher’s config in VsCode
and it worked.
answered Jan 25, 2022 at 13:22
SeleMSeleM
9,2305 gold badges32 silver badges51 bronze badges
Установлен последний питон, selenium, pip.
Через Pycharm пытаюсь запустить элементарный тест
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(‘C:\Python\123\chromedriver.exe’)
driver.get(«www.google.com»)
На что выдаётся ответ
«C:PythonvenvScriptspython.exe C:/Python/123/123.py
Traceback (most recent call last):
File «C:/Python/123/123.py», line 2, in
from selenium.webdriver.common.keys import Keys
ModuleNotFoundError: No module named ‘selenium.webdriver.common’
Process finished with exit code 1″
В IDLE всё выполняется правильно.
Если убрать вторую строку, получаем
«AttributeError: module ‘selenium.webdriver’ has no attribute ‘Chrome'»
When using Selenium with Python, you may receive an error message that says ModuleNotFoundError: No module named ‘selenium’ in Python. This is quite frustrating. This article will show you how to fix this error and get back to writing your Selenium scripts.
What is Selenium in Python?
Selenium is one of the most powerful open-source testing tools for Web application testing available today. Most browsers, including Internet Explorer, Mozilla Firefox, Chrome, Safari, and Opera, and most operating systems, including Windows, Mac, and Linux, can run the Selenium script.
The Selenium module is not installed
This error can occur if you attempt to run a Selenium test in Python without the selenium module installed. Examine the code below to understand the error better.
# Import but not install the Selenium module from selenium import webdriver # Try using Selenium to get the title of LearnShareIT PATH = 'C:Program Files (x86)chromedriver.exe' drv = webdriver.Chrome(PATH) drv.get('https://learnshareit.com/') print(drv.title) drv.quit()
Error:
ModuleNotFoundError: No module named 'selenium'
We can fix this error by installing the Selenium module. This can be done with the package installer for Python(pip).
Pip is a Python package manager that allows you to install and manage Python packages such as Selenium. Simply type the following into your terminal to install Selenium using pip:
pip install selenium
Use the following command line if you’re using Python 3 on Linux or macOS:
pip3 install selenium
Or, if you’re using Anaconda, use the following command line:
conda install -c conda-forge selenium
These commands will download and install the latest version of Selenium.
If you see a message starting with “Successfully installed”. That means you’ve installed Selenium successfully. You can now use Selenium with Python without getting this error.
# Installed Selenium module from selenium import webdriver # Try using Selenium to get the title of LearnShareIT PATH = 'C:Program Files (x86)chromedriver.exe' drv = webdriver.Chrome(PATH) drv.get('https://learnshareit.com/') print(drv.title) drv.quit()
Output:
LearnShareIT - Let's learn and share knowledge about IT
Using the wrong name when importing
If you import the name ‘Selenium’ into the program, Python will throw an error like the sample code below.
# Import with the name 'Selenium' from Selenium import webdriver
Error:
ModuleNotFoundError: No module named 'Selenium'
This is a common spelling error made by Python beginners who are unfamiliar with modules. We must avoid using names that contain uppercase letters when importing the Selenium module. To fix the error in this case, lowercase all characters when importing. Like this:
# Import with the lowercase name 'selenium' from selenium import webdriver # Try using Selenium to get the title of LearnShareIT PATH = 'C:Program Files (x86)chromedriver.exe' drv = webdriver.Chrome(PATH) drv.get('https://learnshareit.com/') print(drv.title) drv.quit()
Output:
LearnShareIT - Let's learn and share knowledge about IT
Summary
We have clarified some causes and solutions for the ModuleNotFoundError: No module named ‘selenium’ above. The main cause of this error is that the Selenium module has not been installed. Therefore, if you encounter this or a similar error in the future, make sure you have all of the required modules installed.
Happy coding!
Maybe you are interested:
- No module named ‘flask_sqlalchemy’ in Python
- ModuleNotFoundError: No module named ‘OpenSSL’ in Python
- No module named ‘flask_restful’ in Python
Hi, I’m Cora Lopez. I have a passion for teaching programming languages such as Python, Java, Php, Javascript … I’m creating the free python course online. I hope this helps you in your learning journey.
Name of the university: HCMUE
Major: IT
Programming Languages: HTML/CSS/Javascript, PHP/sql/laravel, Python, Java
Python shows no module named selenium
error when it can’t find the selenium
library you imported in your code.
This can mean that you don’t have selenium installed, or you installed it for a different version of Python (if you have multiple Python versions)
This article shows simple steps you can do to fix this error.
Why no module named selenium error occurs
This error occurs when you try to import the selenium
library in your code like this:
import selenium
#or
from selenium import webdriver
When Python can’t find the library, it raises the following error:
Traceback (most recent call last):
File ...
import selenium
ModuleNotFoundError: No module named 'selenium'
Because selenium
isn’t a standard Python library, you need to install it using pip
before you can import it.
Here’s the command to install selenium with pip:
pip install selenium
# or if you have Python 3
pip3 install selenium
Once you have selenium
library installed, you should be able to run the code without any errors.
If that doesn’t work, then you may have installed multiple versions of Python on your computer.
One of the following scenarios may happen in your case:
- You have multiple versions of Python installed on your system, and you are using a different version of Python than the one where
selenium
is installed. - You might have
selenium
installed in a virtual environment, and you are not activating the virtual environment before running your code. - Your IDE uses a different version of Python from the one that has
selenium
installed
Let’s see how to fix these errors.
Case#1 — You have multiple versions of Python
If you have multiple versions of Python installed on your system, you need to make sure that you are using the specific version where the selenium
package is installed.
You can test this by running the which -a python
or which -a python3
command from the terminal:
$ which -a python
/opt/homebrew/bin/python
/usr/bin/python
In the example above, there are two versions of Python installed on /opt/homebrew/bin/python
and /usr/bin/python
.
Suppose you run the following steps in your project:
- Install
selenium
withpip
using/usr/bin/
Python version - Install Python using Homebrew, now you have Python in
/opt/homebrew/
- Then add
import selenium
in your code
The steps above will cause the error because selenium
is installed in /usr/bin/
, and your code is probably executed using Python from /opt/homebrew/
path.
To solve this error, you need to run pip install selenium
command again so that the package is installed and accessible by the new Python version.
Finally, keep in mind that you can also have pip
and pip3
available on your computer.
These days, the pip
command is used to install modules for Python 2, while pip3
installs for Python 3. Make sure that you are using the right command for your situation.
Next, you can also have the package installed in a virtual environment.
Case#2 — You are using Python virtual environment
Python venv
package allows you to create a virtual environment where you can install different versions of packages required by your project.
If you are installing selenium
inside a virtual environment, then the module won’t be accessible outside of that environment.
Even when you never run the venv
package, Python IDE like Anaconda and PyCharm usually create their own virtual environment when you create a Python project with them.
You can see if a virtual environment is activated or not by looking at your command prompt.
When a virtual environment is activated, the name of that environment will be shown inside parentheses as shown below:
In the picture above, the name of the virtual environment (base)
appears when the Conda virtual environment is activated.
To solve this, you can either:
- Turn off the virtual environment so that
pip
installs to your computer - Install the
selenium
in the virtual environment withpip
You can choose the solution that works for your project.
When your virtual environment is created by Conda, run the conda deactivate
command. Otherwise, running the deactivate
command should work.
To activate your virtual environment, use one of the following commands:
# For Conda:
conda activate <env_name>
# For venv:
source <env_name>/bin/activate
For Pycharm, you need to follow the Pycharm guide to virtual environment.
Case#3 — IDE using a different Python version
Finally, the IDE from where you run your Python code may use a different Python version when you have multiple versions installed.
If you use VSCode, run the steps below to check available Python interpreters:
- Open the command palette (
CTRL + Shift + P
for Windows and⌘ + Shift + P
for Mac) - Run the
Python: Select Interpreter
command.
You should see all available Python versions listed as follows:
You need to use the same version where you installed selenium
so that the module can be found when you run the code from VSCode.
If you use Pycharm, follow the Pycharm configuring Python interpreter guide.
Once done, the import selenium
statement shouldn’t cause any error.
Conclusion
To conclude, the ModuleNotFoundError: No module named 'selenium'
error occurs when the selenium
package is not available in your Python environment.
To fix this error, you need to install selenium
using pip
.
If you already have the module installed, make sure you are using the correct version of Python, activate the virtual environment if you have one, and check for the Python version used by your IDE.
By following these steps, you should be able to import selenium
modules without any error.
Problem:
Solution#
1. Please check the project interpreter configured. See below example to locate it on Pycharm
2. If 1 does not work, pip uninstall selenium and pip install -U selenium ( I observed it with some users that though they installed selenium not all required files are installed, I would recommend you to uninstall and install selenium)
3.if both do not work for you, read below links from stackoverflow to try some more options:
https://stackoverflow.com/questions/7426851/trying-to-use-selenium-2-with-python-bindings-but-im-getting-an-import-error/51345868#51345868
https://stackoverflow.com/questions/49482586/pycharm-referenced-error-with-import-selenium-webdriver
https://stackoverflow.com/questions/45645838/python-having-errors-with-selenium-webdriver?rq=1
While trying each step of the solution, keep running the below script using your favorite IDE:
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.options import Options import time opts = Options() prefs = {"profile.managed_default_content_settings.images": 2} opts.add_experimental_option("prefs", prefs) # enter complete path of chrome driver as argument to below line of code browser = webdriver.Chrome('C:\Users\BLR153\AppData\Local\Programs\Python\Python36-32\selenium\chromedriver.exe') # browser = webdriver.Firefox() browser.get('http://www.udemy.com') time.sleep(10) browser.quit()
Do you have any suggestions or comments on this post, please leave a comment/suggestion.