53

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?

mx0
  • 6,445
  • 12
  • 49
  • 54
Deepa
  • 561
  • 1
  • 5
  • 7

7 Answers7

182

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
Mesut GUNES
  • 7,089
  • 2
  • 32
  • 49
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Kousik Sep 28 '15 at 13:46
  • 11
    My answer is correct and solves the problem. Did you test it? – Mesut GUNES Sep 28 '15 at 14:57
  • 1
    @MesutGüneş: The OP stated in a comment on the other answer: _"No I have not created selenium file."_ So, the solution you present here does not apply to the OP's problem. – honk Sep 28 '15 at 16:40
  • 1
    Lol, comes handy when drunk! – Chris Russo Jun 17 '18 at 21:00
  • 4
    worked for me as well, wouldn't have guessed this is such a common mistake – tanvi Apr 11 '19 at 19:42
  • 4
    Nice solution. That's really funny LOL – Hypernova May 01 '20 at 11:25
  • 3
    Your answer solved my issue - I had a file called selenium.py in same directory :-) – Sudheer Muhammed Dec 11 '20 at 13:06
  • **Same if your python script name is "signal.py", I am currently creating a script for signal and changed the name to signal.py and it didn't work anymore. I renamed it to "signalbot.py" and it was good.** – LinkPhoenix Mar 09 '21 at 21:00
  • 1
    Omg, this happens even after years of programming experience. Logged in just to say thank you – sammy Jul 06 '23 at 15:54
22

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 ?

amow
  • 2,203
  • 11
  • 19
  • Two folders (selenium and selenium-2.45.0-py3.5.egg-info) are created in C:\Program Files\Python 3.5\Lib\site-packages path. Is this correct or I have done any mistakes during installation? – Deepa Mar 17 '15 at 07:11
  • 3
    No I have not created selenium file. Please help me to solve this problem. – Deepa Mar 17 '15 at 07:24
  • import selenium ,and then print selenium.__file__ . give me the output – amow Mar 17 '15 at 09:14
  • output is Finding files... done. Importing test modules ... done. ---------------------------------------------------------------------- Ran 0 tests in 0.000s OK – Deepa Mar 17 '15 at 09:18
  • did you do that in python shell? – amow Mar 17 '15 at 09:20
  • No, I did it in eclipse. – Deepa Mar 17 '15 at 09:22
  • In eclipse the oupput is Finding files... done. Importing test modules ... C:\Program Files\Python 3.5\Lib\site-packages\selenium\selenium.py done. ---------------------------------------------------------------------- Ran 0 tests in 0.000s OK and in python shell the output is C:\Program Files\Python 3.5\Lib\site-packages\selenium\__init__.py – Deepa Mar 17 '15 at 09:28
  • did you see the **webdriver** folder under **site-packages\selenium** ? Is there a **__init__.py** file under **site-packages\selenium** ? – amow Mar 17 '15 at 09:32
  • Yes webdriver folder and __init__.py file is there under site-packages\selenium. – Deepa Mar 17 '15 at 09:37
  • I cant find the solution. It seems like everything is good.Sorry – amow Mar 17 '15 at 09:42
15
  1. if your file is selenium.py rename it.
  2. Pay attention to have import Keys instead of import keys
Matheew
  • 295
  • 3
  • 14
8

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

deepak
  • 1,081
  • 12
  • 18
2

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
ljmocic
  • 1,677
  • 2
  • 18
  • 23
0

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

CVS
  • 13
  • 1
  • 6
0

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

Enver
  • 542
  • 5
  • 7