我在python中导入selenium webdriver时遇到了一些麻烦。只是为了确保:这是我的小脚本:
import selenium
from selenium import webdriver
browser = webdriver.Firefox(executable_path='/Users/Sleeps/Webdrivers/Firefox/geckodriver')
当我跑步时
from selenium import webdriver
,
我抛出了错误
No module named 'selenium.webdriver.firefox.webdriver'
。
我整齐地按照Selenium自己建议的步骤(你可以找到它们here)。 Nonethelss,我得到了错误。我不知道为什么会这样。我尝试了以下方法:
pip install selenium
python -m pip install -U selenium
sudo pip3 install selenium
无论我使用什么,pip
都会确认
Requirement already satisfied: selenium in ./anaconda/lib/python3.5/site-packages
我在Anaconda目录中进一步讨价还价。我似乎确实在适当的地方有selenium包:/Users/Sleeps/anaconda/lib/python3.5/selenium/webdriver/__init__.py
修改
我进一步为MacOS安装了最新的gecko驱动程序(你可以找到它here)。我将其解压缩到Users/Sleeps/Webdrivers/Firefox/
。
我在命令行中运行PATH=$PATH:/Users/Sleeps/Webdrivers/Firefox/geckodriver
。
运行from pprint import pprint; import os; import sys; pprint(sys.path); pprint(os.getenv("PATH")); pprint(os.getcwd())
返回
['',
'/Users/Sleeps/anaconda/lib/python3.5/site-packages/six-1.10.0-py3.5.egg',
'/Users/Sleeps/anaconda/lib/python35.zip',
'/Users/Sleeps/anaconda/lib/python3.5',
'/Users/Sleeps/anaconda/lib/python3.5/plat-darwin',
'/Users/Sleeps/anaconda/lib/python3.5/lib-dynload',
'/Users/Sleeps/anaconda/lib/python3.5/site-packages',
'/Users/Sleeps/anaconda/lib/python3.5/site-packages/Sphinx-1.4.1-py3.5.egg',
'/Users/Sleeps/anaconda/lib/python3.5/site-packages/aeosa',
'/Users/Sleeps/anaconda/lib/python3.5/site-packages/IPython/extensions',
'/Users/Sleeps/.ipython']
'/Library/Frameworks/Python.framework/Versions/2.7/bin:/Users/Sleeps/anaconda/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'
('/Users/Sleeps/Dropbox/01_Data '
'Science/Kooperation/EX/Automobile‘)
ls -alh
返回
total 24
drwxr-xr-x@ 3 Sleeps staff 102B 9 Aug 12:06 .
drwxr-xr-x@ 35 Sleeps staff 1,2K 9 Aug 12:06 ..
-rwxr-xr-x@ 1 Sleeps staff 9,4K 9 Aug 11:59 Test.ipynb
当我运行from selenium import webdriver
时,我仍然会收到错误消息。
我怎么能摆脱它?
答案 0 :(得分:1)
请尝试以下方法: -
在Ubuntu / Debian系统上,这将安装pip(和依赖项),然后从PyPI安装Selenium Python绑定:
$ sudo apt-get install python-pip
$ sudo pip install selenium
安装后,以下代码应该有效:
#!/usr/bin/env python
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.ubuntu.com/')