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.
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.
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!
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
错误ImportError: cannot import name webdriver or no module selenium2library
通过将selenium文件夹直接放在Lib
而不是site_packages
下site_packages
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__)
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()
Hope that helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.