Trying to use Selenium 2 with Python bindings, but I'm getting an import error


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()
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
assert "Google" in driver.title

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

Traceback (most recent call last):
  File "", 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.

9/15/2011 6:43:39 AM

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 "", 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 "" 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.

10/1/2015 12:36:32 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow