44
votes

I am a newbie for selenium python. I have installed python, pip etc.. I am trying to run the below code but it is showing error:

ImportError: cannot import name 'webdriver'

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

driver = webdriver.Firefox()
driver.get("http://www.python.org")

could anyone please solve this issue?

7

7 Answers

145
votes

If your file name is selenium.py, change it to something else and delete .pyc files or the __pycache__ directory if exists.

If you do not have the selenium, you should install the selenium by using pip or pipenv:

pip install selenium
19
votes

It says that webdriver cant be import.So I assume you have Selenium installed.

So I can only assume that the selenium lied in different place in your path.

Maybe you have accidently create a file named selenium ?

5
votes
  1. if your file is selenium.py rename it.
  2. Pay attention to have import Keys instead of import keys
4
votes

Step 1: First rename filename if saved with selenium.py and delete selenium.pyc. mv selenium.py test.py rm selenium.pyc Step 2: import module selenium if not already installed. pip install selenium

2
votes

I solved it by reinstalling the older version of selenium package because the newest version doesn't support Python 2.6.6 which in my case was installed and I didn't have root access to install the new one.

While the newest version of selenium package doesn't have support for Python 2.6.6, I had to downgrade by reinstalling the selenium package with lower version

pip uninstall selenium
pip install --user selenium==3.5.0
0
votes

Deepa, it is likely a 'path not found' issue. Add as many paths as relevant to help the IDE find selenium and webdriver. For example, on my Mac, I've added all the following paths so nothing is missed :

file:///Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5 file:///Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin file:///Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload file:///Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages file:///Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium-3.0.0b2/py file:///Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium-3.0.0b2/py/selenium file:///Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium-3.0.0b2/py/selenium/common file:///Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium-3.0.0b2/py/selenium/webdriver

0
votes

I have deleted pycache folder and renamed to my py file, now it works without any error.