Python 带有 Selenium 错误的 PhantomJS:消息:'phantomjs' 可执行文件需要在 PATH 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37903536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:06:14  来源:igfitidea点击:

PhantomJS with Selenium error: Message: 'phantomjs' executable needs to be in PATH

pythonseleniumselenium-webdriverphantomjs

提问by quantumbutterfly

I am attempting to run this script:

我正在尝试运行此脚本:

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

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

However, the script fails at the line session = webdriver.PhantomJS()with the following error

但是,脚本在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

How do I add phantomjs to my PATH? I am running ubuntu 16.04 and installed selenium via npm install selenium.

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

采纳答案by Ourik gruzdev

I solved same promlem with this command in command line

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

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

It's work if /home/login/phantomjs/bin is the path for folder with executable 'phantomjs'.

如果 /home/login/phantomjs/bin 是具有可执行文件 'phantomjs' 的文件夹的路径,它就可以工作。

回答by Leon Barkan

you need to download the DRIVER

您需要下载驱动程序

after that session = webdriver.PhantomJS("c:\driverPath")

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

回答by Dung

Working Solution:

工作解决方案:

Assumming you are on windows - it is similar for linux

假设您在 Windows 上 - 它与 linux 类似

1) download phantomjs here: http://phantomjs.org/download.htmlpick windows/linux accordingly

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

2) unzip your phantomjs-2.1.1-windows.zip and save it to for example c drive such as C:\phantomjs-2.1.1-windows\bin (in here there is a phantomjs.exe that is the execute that your system needs)

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

3) On Windows10 edit your environment path to include this bin folder C:\phantomjs-2.1.1-windows\bin such as this example enter image description here

3) 在 Windows10 上编辑您的环境路径以包含此 bin 文件夹 C:\phantomjs-2.1.1-windows\bin 如本例 在此处输入图片说明

4) you may or may not restart your machine. Done! it should work! (Webdriver looks for phantomjs.exe and it should be ready now)

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

回答by Nandesh

You need to provide the executable path.This is for linux or more precisely Ubuntu.

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

You should specify the executable file path(complete), not the directory path that contains the executable.

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

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

It does not require any drivers.

它不需要任何驱动程序。

Worked well for me on Ubuntu 16.04.

在 Ubuntu 16.04 上对我来说效果很好。

回答by Ahmed Soliman

Why Don't you use the easiest way ever and past the phantomjs.exe

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

into the Python scripts directory which is already added to the system environment the python directory path should be something like this

进入已经添加到系统环境中的 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()

回答by Guru

1.Download Phantomjs executable from https://phantomjs.org/download.html2. copy phantomjs.exe to C:\Python27\Lib\site-packages\selenium\webdriver\phantomjs 3. Add path C:\Python27\Lib\site-packages\selenium\webdriver\phantomjs under environment variable key "PATH"

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

since I am using PyCharm I had to restart Pycharm after above settings are done

由于我使用的是 PyCharm,因此我必须在完成上述设置后重新启动 Pycharm