简体   繁体   English

尝试使用Selenium 2与Python绑定,但我收到导入错误

[英]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_packagessite_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()
  1. Make sure that you have only one python version installed 确保安装了一个python版本
  2. Install pip 安装点子
  3. Install selenium using pip 使用pip安装硒
    pip install selenium pip安装硒
  4. Run the script 运行脚本

Hope that helps. 希望有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试在 python 中使用 HTML img 标签发送邮件,但出现以下错误 - I'm trying to use HTML img tag in python to send a mail, but I'm getting the following error selenium.common.exceptions.TimeoutException:当我尝试使用python进行按钮单击时,将出现此错误 - selenium.common.exceptions.TimeoutException: this error will getting when I'm trying to button click using python 尝试使用鼠标/导入时,出现递归错误/package 操作失败 - When trying to use mouse / import, I'm getting a recursion error / package operation fail 我在尝试从urlresolver导入保留时遇到错误 - I'm getting error when trying to import reserve from urlresolver Python Selenium-我正在尝试使用pytest框架,遇到错误 - Python Selenium - I'm trying to use pytest framework , running into errors 我正在尝试在 ubuntu 上的崇高文本中使用 python 上的 tkinter 但我一直收到错误 - I'm trying to use tkinter on python in sublime text on ubuntu but i keep getting a error 在 Python 中,我试图在一个返回值的函数中导入一个单独的模块文件,但一直出错,有什么想法吗? - in Python, I'm trying to import a separate module file within a value-returning function but keep getting an error, any ideas? 我在 Bokeh 中尝试使用 ColumnDataSource 时遇到错误 - I'm getting and error trying to use ColumnDataSource in Bokeh 我正在尝试使用此 python 代码创建 wordcloud,但不断出现错误 - I'm trying to create a wordcloud with this python code, but keeps getting error 尝试将urllib与标头一起使用,但我不断收到此错误:TypeError:需要浮点数。 我正在使用python 3.4 - Trying to use urllib with headers but I keep on getting this error: TypeError: a float is required. I'm using python 3.4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM