Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
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.

like image 216
Cass Avatar asked Sep 15 '11 06:09

Cass


People also ask

Why import is not working in selenium?

It sounds like you might have downloaded the Selenium RC , but not Selenium Webdriver . For from selenium import webdriver to work, you must install Selenium Webdriver .

What version of Python does selenium work with?

Selenium Python bindings provide a convenient API to access Selenium WebDrivers like Firefox, Ie and Chrome. The current supported Python versions are Python 2.6 and Python 2.7.

What is selenium Python bindings?

What are Selenium-Python Bindings? Selenium Python bindings provide an easy to use API to write functional acceptance tests using Selenium WebDriver. The Selenium Python API allows users to access various functionalities of the Selenium WebDriver in an intuitive way.

How do I import into selenium?

Import a Selenium IDE project​From the menu bar, select File > Import Selenium IDE Project and browse your Selenium IDE file (a single file with a . side extension) to open.


1 Answers

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.

like image 119
Bryan Oakley Avatar answered Oct 13 '22 01:10

Bryan Oakley