AttributeError:'module'对象没有属性'PhantomJS'

时间:2015-03-10 14:41:59

标签: python macos selenium phantomjs

>>> from selenium import webdriver
>>> browser = webdriver.PhantomJS()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'PhantomJS'

我似乎无法在OSX上使用它。

我用pip install selenium安装了selenium,我有这个版本:

>>> import selenium
>>> selenium.__version__
'2.21.2'

我使用port install phantomjs安装了PhantomJS,并验证它是最新的:

$ phantomjs --version
2.0.0

我检查了它的位置是在我的PATH和我的PYTHONPATH中,重新启动了我的终端等,但这并没有解决错误。

browser = webdriver.Firefox()效果很好。

编辑:

我似乎指的是旧版本 - 2.21.2 - 通过MacPorts安装,但我仍然无法解决问题。

我到目前为止:

sudo pip uninstall selenium
sudo port uninstall py-selenium
sudo port uninstall py27-selenium

而且我试图从pip重新安装最新的(2.45.0):

$ sudo pip install selenium
Downloading/unpacking selenium
  Downloading selenium-2.45.0.tar.gz (2.6MB): 2.6MB downloaded
  Running setup.py (path:/private/tmp/pip_build_root/selenium/setup.py) egg_info for package selenium

Installing collected packages: selenium
  Running setup.py install for selenium

Successfully installed selenium
Cleaning up...

但现在我甚至无法输入硒:

>>> import selenium
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named selenium

编辑2:

看来pip指向错误的python。我将pip的第一行更改为正确的路径,但我不确定这是否是我想要的,现在我尝试pip install selenium时只是崩溃:

Traceback (most recent call last):
  File "/usr/local/bin/pip", line 5, in <module>
    from pkg_resources import load_entry_point
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/distribute-0.6.14-py2.7.egg/pkg_resources.py", line 2671, in <module>
    working_set.require(__requires__)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/distribute-0.6.14-py2.7.egg/pkg_resources.py", line 654, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/distribute-0.6.14-py2.7.egg/pkg_resources.py", line 552, in resolve
    raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: pip==1.5.6

1 个答案:

答案 0 :(得分:1)

我的问题是pip没有指向正确的python安装。

首先,我按照“编辑”中的说明删除了所有不需要的硒安装。

然后我使用rm /usr/local/bin/pip删除了点子,并通过sudo port install py27-pip进行了全新安装。

然后我跑了sudo pip install selenium

现在一切正常。

>>> import selenium
>>> selenium.__version__
'2.45.0'
>>> from selenium import webdriver
>>> browser = webdriver.PhantomJS()
>>>