Selenium Python无头Webdriver(PhantomJS)不工作

2024-05-20 17:22:03 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我很难让selenium和一个无头驱动程序一起工作,特别是PhantomJS。我试图让它在Ubuntu网络服务器(Ubuntu 14.04.2lts)上运行。

从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),但也遇到了类似的问题。

我愿意接受任何建议,如果需要的话,我不介意换司机。

提前谢谢你的帮助。


Tags: toinpylibpackagesusrlocaldist
2条回答

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

sudo apt-get install phantomjs

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

我和你有同样的问题,也有同样的错误。我试过在openSuse服务器上安装它。我最终安装了PhantomJS表单源代码-不幸的是没有成功。我的工作方式是通过npm安装Phantomjs

sudo npm install -g phantomjs

相关问题 更多 >