Selenium Python Headless Webdriver (PhantomJS) 不工作

Selenium Python Headless Webdriver (PhantomJS) Not Working

所以我无法让 selenium 与无头驱动程序一起工作,特别是 PhantomJS。我正在尝试让它在 Ubuntu 网络服务器(Ubuntu 14.04.2 LTS)上运行。

运行 来自 python 解释器 (Python 2.7.6) 的以下命令给出:

from selenium import webdriver
driver = webdriver.PhantomJS()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/phantomjs/webdriver.py", line 51, in __init__
    self.service.start()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/phantomjs/service.py", line 76, in start
    raise WebDriverException("Unable to start phantomjs with ghostdriver: %s" % e)
selenium.common.exceptions.WebDriverException: Message: Unable to start phantomjs with ghostdriver: [Errno 2] No such file or directory

我也试过:

driver = webdriver.PhantomJS(executable_path="/usr/local/lib/python2.7/dist-packages/selenium/webdriver/phantomjs/")

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/phantomjs/webdriver.py", line 51, in __init__
    self.service.start()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/phantomjs/service.py", line 76, in start
    raise WebDriverException("Unable to start phantomjs with ghostdriver: %s" % e)
selenium.common.exceptions.WebDriverException: Message: Unable to start phantomjs with ghostdriver: [Errno 13] Permission denied

我也把它添加到python路径:

import sys
sys.path.append("/usr/local/lib/python2.7/dist-packages/selenium/webdriver/phantomjs/")

我目前以根用户身份登录。 phantomjs 目录的权限是:

drwxr-sr-x  2 root staff 4096 Sep  9 06:58 phantomjs

和 phantomjs/webdriver.py:

-rw-r--r--  1 root root  2985 Sep  9 06:58 webdriver.py

我已确认 selenium 已安装并且是最新的(pip install selenium --upgrade)。它安装在:

/usr/local/lib/python2.7/dist-packages/selenium/webdriver/phantomjs/

print selenium.__version__
2.47.1

我看过:

我一直在使用 chromedriver 在本地托管的服务器上(在 OSX 上)测试我的程序。我实际上为此使用了 Splinter (https://splinter.readthedocs.org/en/latest/#headless-drivers),并尝试了其他无头驱动程序(django 和 zope.testbrowser),但遇到了类似的问题。

我乐于接受任何建议,如果需要,我不介意更换驱动程序。

在此先感谢您的帮助。

所以通过包管理器安装 phantomjs 似乎可以解决问题...

sudo apt-get install phantomjs

无论如何,希望上面的过程能帮助其他有同样问题的人。

我遇到了和你一样的问题,同样的错误。我试图将它安装在 openSuse 服务器上。我最终安装了 PhantomJS 表单源 - 不幸的是没有任何成功。对我有用的方法是通过 npm

安装 Phantomjs
sudo npm install -g phantomjs