17

I just installed Selenium 2 by doing pip install selenium and just copied some example tests to make sure that it's working:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)
assert "Google" in driver.title
driver.close()

I saved that as test.py in a sub-folder in my Home folder on my Mac, but when ever I run python test.py, I get the following output:

Traceback (most recent call last):
  File "demo.py", line 1, in <module>
    from selenium import webdriver
ImportError: cannot import name webdriver

If I move that file into my Home directory, it works. If you couldn't tell, I'm just getting started with Selenium and programming. Any help with this would be much appreciated.

8 Answers 8

62

It sounds like you have some other module in your path named "selenium", and python is trying to import that one because it comes earlier in your python path. Did you name your file "selenium.py", for example?

To debug, import selenium with a simple import selenium then print the name of the file that was imported with print selenium.__file__

If you have a file named "selenium.py" which is not the proper selenium library, in addition to renaming or removing it, make sure you also delete "selenium.pyc", or python will continue to try to import from the .pyc file.

4
  • The output I got from that was: /Library/Python/2.7/site-packages/selenium/__init__.pyc Does that look correct?
    – Cass
    Commented Sep 15, 2011 at 17:03
  • Yes, that looks correct. Is that after you moved it to your home directory or before? Make sure it's in the place where you get the import error, otherwise you're just proving what you already know -- that the version in your home directory works. Commented Sep 16, 2011 at 1:29
  • 19
    Thank you for this, helped me when I was doing testing in a file i (stupidly) named selenium.py ... :) Commented Feb 9, 2012 at 14:03
  • 2
    in my case I accidentally named my py file selenium.py
    – Gal Bracha
    Commented Jan 16, 2015 at 8:56
29

Old question, but I did the same thing too. Named my file 'selenium.py' and it gave this very error message. Renamed the file to something else, but still got the same error. The problem was, that the selenium.pyc file had been created, since I ran the script from the terminal. Removed the .pyc file and it ran like a charm!

3
  • I had also to close the terminal and open a new one, so the changes took effect
    – Ander2
    Commented Oct 16, 2013 at 8:33
  • 3
    +1 I found exactly this issue when I mistakenly named my file selenium.py - the resolution was to delete the selenium.pyc file as described Commented Apr 23, 2014 at 6:23
  • This needs more upvotes. Here I was fumbling around with my paths and environments for an hour, and all along it was because I named the file selenium.py
    – Trevor Bye
    Commented Mar 7, 2019 at 22:19
5

Though the question seems to be inactive quite a long time, I had the same message/similar problem, and none of the answers above fit.

The site http://kevingann.blogspot.de/2012/11/troubleshooting-pydev-and-selenium.html gave the crucial hint.

Selenium occured twice, once in the system libs as egg, and the "installed" version in the external libs. Smashing the egg did the trick.

Hope this will help someone too

1
  • yep, that helped, had selenium folder in c:\Anaconda3\selenium with just this contents in it, everything was working, but Pycharm always scream out yellow warnings, for example, that 'cannot find reference PhantomJS in imported module selenium.webdriver': And looks like it shadowed this one: c:\Anaconda3\Lib\site-packages\selenium\ After deleting c:\Anaconda3\selenium there is no warnings in pycharm Commented Oct 14, 2016 at 8:32
3

the error ImportError: cannot import name webdriver or no module selenium2library was resolved by placing selenium folder directly under Lib instead of site_packages

0

Error in Pycharm "Cannot find reference 'Chrome' in 'imported module selenium.webdriver'" got resolved after copying selenium dir from site-packages to lib. Can be verified as stated above

import selenium
print (selenium.__file__)
0

Sethe project interpreter as actual python.exe

I am able to run successfully the code below:

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.google.com')

time.sleep(10)

browser.quit()
0

Just name your pytnon file NOT selenium.py !!! It is not looking for webdriver in selenium, but in your file, where you are trying to connect it

1
  • 2
    In Bryan's answer: If you have a file named "selenium.py" which is not the proper selenium library, in addition to renaming or removing it, make sure you also delete "selenium.pyc", or python will continue to try to import from the .pyc file.
    – Eric Aya
    Commented Jun 23, 2021 at 9:21
-3
  1. Make sure that you have only one python version installed
  2. Install pip
  3. Install selenium using pip
    pip install selenium
  4. Run the script

Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.