[英]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: 我刚刚通过
pip install selenium
安装了Selenium 2并且只是复制了一些示例测试以确保它正常工作:
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: 我将它作为
test.py
保存在我Mac上的Home文件夹的子文件夹中,但是当我运行python test.py
,我得到以下输出:
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.
如果你不知道,我只是开始使用Selenium和编程。 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. 听起来你的路径中有一些名为“selenium”的其他模块,而python正试图导入那个模块,因为它在你的python路径中更早出现。 Did you name your file "selenium.py", for example?
例如,您是否将文件命名为“selenium.py”?
To debug, import selenium with a simple import selenium
then print the name of the file that was imported with print selenium.__file__
要调试,使用简单的
import selenium
然后打印使用print selenium.__file__
导入的文件的名称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. 如果你有一个名为“selenium.py”的文件,它不是正确的selenium库,除了重命名或删除它之外,请确保你还删除“selenium.pyc”,否则python将继续尝试从.pyc导入文件。
Old question, but I did the same thing too. 老问题,但我也做了同样的事情。 Named my file 'selenium.py' and it gave this very error message.
将我的文件命名为'selenium.py',它给出了这个非常错误的消息。 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.
问题是,selenium.pyc文件已经创建,因为我从终端运行脚本。 Removed the .pyc file and it ran like a charm!
删除.pyc文件,它就像一个魅力!
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. 该网站http://kevingann.blogspot.de/2012/11/troubleshooting-pydev-and-selenium.html给出了至关重要的提示。
Selenium occured twice, once in the system libs as egg, and the "installed" version in the external libs. Selenium发生两次,一次在系统libs中作为egg,并且“安装”版本在外部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. Pycharm中的错误“将selenium目录从site-packages复制到lib后,在'导入的模块selenium.webdriver'中找不到引用'Chrome'”。 Can be verified as stated above
可以如上所述进行验证
import selenium
print (selenium.__file__)
Sethe project interpreter as actual python.exe 将项目解释器设置为实际的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. 希望有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.