Python 导入错误:无法导入名称“webdriver”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29092970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:04:36  来源:igfitidea点击:

ImportError: cannot import name 'webdriver'

python

提问by Deepa

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:

我是 selenium python 的新手。我已经安装了 python、pip 等。我正在尝试运行以下代码,但它显示错误:

ImportError: cannot import name 'webdriver'

导入错误:无法导入名称“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?

有人可以解决这个问题吗?

回答by amow

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

它说不能导入 webdriver。所以我假设你已经安装了 Selenium。

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

所以我只能假设硒位于您路径中的不同位置。

Maybe you have accidently create a file named selenium ?

也许您不小心创建了一个名为 selenium 的文件?

回答by Mesut GUNES

if your file name is seleniumchange it to something else and delete .pyc files too.

如果您的文件名是selenium,请将其更改为其他名称并删除 .pyc 文件。

回答by CVS

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 :

Deepa,这很可能是“找不到路径”的问题。添加尽可能多的相关路径以帮助 IDE 找到 selenium 和 webdriver。例如,在我的 Mac 上,我添加了以下所有路径,因此不会遗漏任何内容:

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

file:///Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5 file:///Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin 文件: ///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

回答by deepak

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

第 1 步:首先重命名文件名,如果保存selenium.py并删除selenium.pycmv selenium.py test.py rm selenium.pyc 第 2 步:如果尚未安装,请导入模块 selenium。 pip install selenium

回答by ljmocic

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.

我通过重新安装旧版本的 selenium 包解决了这个问题,因为最新版本不支持 Python 2.6.6,在我的情况下安装了 Python 2.6.6,我没有安装新版本的 root 访问权限。

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

虽然最新版本的 selenium 包不支持 Python 2.6.6,但我不得不通过重新安装较低版本的 selenium 包来降级

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