【问题标题】:PhantomJS with Selenium error: Message: 'phantomjs' executable needs to be in PATH带有 Selenium 错误的 PhantomJS:消息:“phantomjs”可执行文件需要在 PATH 中
【发布时间】:2016-10-20 14:07:21
【问题描述】:

我正在尝试运行此脚本:

https://github.com/Chillee/coursera-dl-all

但是,脚本在session = webdriver.PhantomJS() 行失败,并出现以下错误

Traceback (most recent call last):
  File "dl_all.py", line 236, in <module>
    session = webdriver.PhantomJS()
  File "/home/<user>/.local/lib/python2.7/site-packages/selenium/webdriver/phantomjs/webdriver.py", line 51, in __init__
    self.service.start()
  File "/home/<user>/.local/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 69, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'phantomjs' executable needs to be in PATH. 

Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of <selenium.webdriver.phantomjs.service.Service object at 0x7f6f632864d0>> ignored

如何将 phantomjs 添加到我的 PATH 中?我正在运行 ubuntu 16.04 并通过 npm install selenium 安装了 selenium。

【问题讨论】:

  • phantomjs 安装了吗?
  • 应该是。有什么办法可以复查吗?
  • which phantomjs。如果它不存在,只需将其移动到 /usr/bin 或 PATH 中的任何其他位置

标签: python selenium selenium-webdriver phantomjs


【解决方案1】:

您需要下载DRIVER

之后session = webdriver.PhantomJS("c:\driverPath")

【讨论】:

  • 在 Windows 上它应该看起来像 session = webdriver.PhantomJS(executable_path='C:\\Phantomjs\\bin\\phantomjs') 其中最后一个 'phantomjs' 代表 phantomjs.exe
  • 在 PhantomJS 上工作了几个小时以找出问题所在后,我遇到的最后一个错误是“Selenium 对 PhantomJS 的支持已被弃用”。向我学习,不要在这个模块上浪费你宝贵的时间。
【解决方案2】:

工作解决方案:

假设您在 Windows 上 - 对于 linux 类似

1) 在此处下载 phantomjs:http://phantomjs.org/download.html 相应地选择 windows/linux

2) 解压缩您的 phantomjs-2.1.1-windows.zip 并将其保存到例如 C 驱动器,例如 C:\phantomjs-2.1.1-windows\bin (这里有一个 phantomjs.exe 是执行您的系统需要)

3) 在 Windows10 上,编辑您的环境路径以包含此 bin 文件夹 C:\phantomjs-2.1.1-windows\bin,例如此示例

4) 您可能会也可能不会重新启动您的机器。完毕!它应该工作! (Webdriver 查找 phantomjs.exe,它现在应该准备好了)

【讨论】:

    【解决方案3】:

    我在命令行中用这个命令解决了同样的问题

    export PATH=${PATH:+$PATH:}/home/<login>/phantomjs/bin
    

    如果 /home/login/phantomjs/bin 是具有可执行文件“phantomjs”的文件夹的路径,则可以。

    【讨论】:

    • 这个命令需要添加到~/.bashrc中。例如:>> sudo nano ~/.bashrc;然后在添加上面的命令之后我们需要做:>> source ~/.bashrc
    【解决方案4】:

    您需要提供可执行路径。这适用于 linux 或更准确地说是 Ubuntu。

    您应该指定可执行文件路径(完整),而不是包含可执行文件的目录路径。

    driver = webdriver.PhantomJS(executable_path='Complete path/to/phantomjs')
    

    它不需要任何驱动程序。

    在 Ubuntu 16.04 上运行良好。

    【讨论】:

      【解决方案5】:

      你为什么不使用最简单的方法和过去的phantomjs.exe

      进入已经添加到系统环境的 Python 脚本目录 python目录路径应该是这样的

      C:\Users\[user]\AppData\Local\Programs\Python\Python[version]\Scripts
      # you can use it as following 
      from selenium import webdriver
      driver = webdriver.PhantomJS()
      

      【讨论】:

        【解决方案6】:

        1.从https://phantomjs.org/download.html下载Phantomjs可执行文件 2.将phantomjs.exe复制到C:\Python27\Lib\site-packages\selenium\webdriver\phantomjs 3.在环境变量键“PATH”下添加路径C:\Python27\Lib\site-packages\selenium\webdriver\phantomjs

        因为我使用的是 PyCharm,所以在完成上述设置后我不得不重新启动 Pycharm

        【讨论】:

          【解决方案7】:

          这将完美运行。

          import platform
          from os import getcwd
          from selenium import webdriver
          
          if (platform.system() == 'Windows'):
          driver = webdriver.PhantomJS(executable_path=getcwd() + "\phantomjs")
          
          if (platform.system() == 'Darwin'):
          driver = webdriver.PhantomJS(executable_path=getcwd() + "/phantomjs")
          

          【讨论】:

            猜你喜欢
            • 2019-06-05
            • 1970-01-01
            • 1970-01-01
            • 2019-03-28
            • 2018-08-18
            • 1970-01-01
            • 2018-03-25
            • 1970-01-01
            • 2018-07-11
            相关资源
            最近更新 更多