ModuleNotFoundError: No module named 'selenium'
Asked Answered
R

8

6

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)

Rugged answered 5/5, 2017 at 5:43 Comment(4)
have you installed selenium ? like pip install selenium?Ulmaceous
I have Selenium Standalone Server version 3.4.0(link). I've also installed ChromeDriver 2.29. Do I need to install anything else?Rugged
the problem is the selenium file is not being loaded. go to cmd and use pip install selenium.Ulmaceous
i'll try that today, thanks.Rugged
A
16

Try installing selenium using pip. Use the following command.

python -m pip install -U selenium
Association answered 5/5, 2017 at 5:57 Comment(4)
Thanks, the above issue is fixed, but i get another error: Traceback (most recent call last): File "C:\Users\admin\Desktop\test2.py", line 7, in <module> driver = webdriver.chrome("<webdriver path>") TypeError: 'module' object is not callableRugged
You need to Download chrome Driver. check the following link : #13725278Association
Your command with the -m and -U worked for me. It didn't work without those flags.Pericycle
Could someone explain why this works? It seems not everyone needs to run it like this for it to work, but others do.Echinoderm
E
6

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
Evildoer answered 15/1, 2020 at 19:51 Comment(0)
K
4

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

Kristankriste answered 3/3, 2019 at 7:29 Comment(1)
Ok in my case I did all the above and tried the 'pip install selenium' and still had trouble. Once I DEACTIVATED 'source deactivate' and then 'source activate' to refresh the virtual environment, I navigated back to my "tests" folder and ran 'pytest' just to run all the tests and BOOM! it worked.Fahey
N
1

driver = webdriver.chrome("")

there is no such class ^^. It is named webdriver.Chrome()

Near answered 20/3, 2018 at 14:54 Comment(0)
A
0

Okay, the Quick and Easy Solution is to Go to Your Python Version Location, Then Libs, and then Site-packages.

ex - C:\Users\Admin\AppData\Local\Programs\Python\Python38\Lib\site-packages

Try Deleting and Reinstalling Selenium, and Try Running the Code.
Anselmi answered 22/4, 2020 at 18:12 Comment(0)
M
0
  1. You can enter these commands "pip install webdriver_manager"
  2. Then "pip install selenium"
Milson answered 24/6, 2020 at 14:49 Comment(0)
P
0

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:

  1. Find path where your Python library exists. (Like C:\Users\cp\AppData\Local\Programs\Python\Python38-32\Lib\site-packages)
  2. Copy site-packages folder.
  3. Go to Project.
  4. Find "Lib" folder.
  5. Expand Lib folder and you will find site_packages over there.
  6. Paste site-packages.

I believe it will help you out.

Peripteral answered 8/8, 2020 at 12:19 Comment(1)
3. Go to Project in Pycharm.Peripteral
C
0

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>\AppData\Local\Microsoft\Python

  • Normal Install (i.e. download and install in a chosen directory, mine was C:\Users\<user>\AppData\Local\Programs\Python)

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.

Crotch answered 25/1, 2022 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.