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,2556 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.6k28 gold badges128 silver badges142 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,2205 gold badges32 silver badges51 bronze badges
ModuleNotFoundError: No module named selenium error occurs if the selenium module is not properly installed or not configured properly. Selenium library provides features to automate the browser which has multiple use cases like automation testing, backend to virtual assistant, etc. Selenium comes with multi-programming language support like Java, Python, etc. In this article, we will specifically focus on solving the error No module named selenium with Python Interpreter.
The best solution is to install or reinstall selenium package to fix this error. But how?
Solution 1: Use pip to install selenium –
Use the below command to use pip for selenium installation.
pip install selenium
Optional Scenario :
1.1. In case the system needs admin privileges to run the above pip command for selenium installation. Then add sudo keyword at the beginning of the command for the Linux family OS. If you are using a Window-based OS, open the command prompt and run the above command only.
sudo pip install selenium
1.2 If you need to install any specific version for selenium all we need to mention in the same command.
pip install selenium==4.5.0
Regarding different versions of selenium with pip, refer selenium release notes on PyPI.
1.3 In case pip is outdated or failing then use the below command as a prerequisite before you run any of the above commands.
python -m ensurepip --default-pip
python -m pip install --upgrade pip setuptools wheel
pip install selenium
1.4 if the path is not set for pip itself then run the below command.
python -m pip install selenium
Solution 2: Use conda to install selenium –
Similar to pip we can use conda to install selenium with the below command.
conda install -c conda-forge selenium
Solution 3: Use source code to install selenium –
Installation for scratch. For this we need the download the source code. Since this source code is generic and has an implementation for multiple programming languages like java, python. Hence we need to first navigate into the Py folder and then build the wheel or any other binding format.
Thanks
DSL Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
We respect your privacy and take protecting it seriously
Thank you for signup. A Confirmation Email has been sent to your Email Address.
Something went wrong.
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
A common error you may encounter when using Python is modulenotfounderror: no module named ‘selenium’.
This error occurs when the Python interpreter cannot detect the Selenium library in your current environment.
This tutorial goes through the exact steps to troubleshoot this error for the Windows, Mac and Linux operating systems.
Table of contents
- ModuleNotFoundError: no module named ‘selenium’
- What is Selenium?
- How to Install Selenium on Windows Operating System
- Selenium installation on Windows Using pip
- How to Install Selenium on Mac Operating System using pip
- How to Install Selenium on Linux Operating Systems
- Installing pip for Ubuntu, Debian, and Linux Mint
- Installing pip for CentOS 8 (and newer), Fedora, and Red Hat
- Installing pip for CentOS 6 and 7, and older versions of Red Hat
- Installing pip for Arch Linux and Manjaro
- Installing pip for OpenSUSE
- Selenium installation on Linux with Pip
- Installing Selenium Using Anaconda
- Check Selenium Version
- Summary
ModuleNotFoundError: no module named ‘selenium’
What is Selenium?
Selenium is a suite of tools for automating web browsers. You can use Selenium to automate web applications for testing purposes, though it is not only for testing.
The simplest way to install Selenium is to use the package manager for Python called pip. The following installation instructions are for the major Python version 3.
How to Install Selenium on Windows Operating System
First, you need to download and install Python on your PC. Ensure you select the install launcher for all users and Add Python to PATH checkboxes. The latter ensures the interpreter is in the execution path. Pip is automatically on Windows for Python versions 2.7.9+ and 3.4+.
You can check your Python version with the following command:
python3 --version
You can install pip on Windows by downloading the installation package, opening the command line and launching the installer. You can install pip via the CMD prompt by running the following command.
python get-pip.py
You may need to run the command prompt as administrator. Check whether the installation has been successful by typing.
pip --version
Selenium installation on Windows Using pip
To install Selenium, run the following command from the command prompt.
pip3 install selenium
How to Install Selenium on Mac Operating System using pip
Open a terminal by pressing command (⌘) + Space Bar to open the Spotlight search. Type in terminal and press enter. To get pip, first ensure you have installed Python3:
python3 --version
Python 3.8.8
Download pip by running the following curl command:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
The curl command allows you to specify a direct download link. Using the -o option sets the name of the downloaded file.
Install pip by running:
python3 get-pip.py
From the terminal, use pip3 to install Selenium:
pip3 install selenium
How to Install Selenium on Linux Operating Systems
All major Linux distributions have Python installed by default. However, you will need to install pip. You can install pip from the terminal, but the installation instructions depend on the Linux distribution you are using. You will need root privileges to install pip. Open a terminal and use the commands relevant to your Linux distribution to install pip.
Installing pip for Ubuntu, Debian, and Linux Mint
sudo apt install python-pip3
Installing pip for CentOS 8 (and newer), Fedora, and Red Hat
sudo dnf install python-pip3
Installing pip for CentOS 6 and 7, and older versions of Red Hat
sudo yum install epel-release
sudo yum install python-pip3
Installing pip for Arch Linux and Manjaro
sudo pacman -S python-pip
Installing pip for OpenSUSE
sudo zypper python3-pip
Selenium installation on Linux with Pip
Once you have installed pip, you can install Selenium using:
pip3 install selenium
Installing Selenium Using Anaconda
First, to create a conda environment to install PIL.
conda create -n selenium python=3.6
Then activate the selenium container. You will see “selenium” in parentheses next to the command line prompt.
source activate selenium
Now you’re ready to install Selenium using conda.
Anaconda is a distribution of Python and R for scientific computing and data science. You can install Anaconda by going to the installation instructions. Once you have installed Anaconda and created your conda environment, you can install Selenium using one of the following commands:
conda install -c conda-forge selenium
Check Selenium Version
Once you have successfully installed Selenium, you can check the version of Selenium. If you used pip to install Selenium, you can use pip show from your terminal.
pip show selenium
Name: selenium
Version: 4.1.0
Second, within your python program, you can import selenium and then reference the __version__ attribute:
import selenium
print(selenium.__version__)
4.1.0
If you used conda to install Selenium, you could check the version using the following command:
conda list -f selenium
# Name Version Build Channel
selenium 3.141.0 py36hfa26744_1002 conda-forge
Summary
Congratulations on reading to the end of this tutorial. The modulenotfounderror occurs if you misspell the module name, incorrectly point to the module path or do not have the module installed in your Python environment. If you do not have the module installed in your Python environment, you can use pip to install the package. However, you must ensure you have pip installed on your system. You can also install Anaconda on your system and use the conda install command to install Selenium.
Go to the online courses page on Python to learn more about Python for data science and machine learning.
For further reading on missing modules in Python, go to the article: How to Solve Python ModuleNotFoundError: no module named ‘urllib2’.
Have fun and happy researching!
Установлен последний питон, 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'»