28

I am attempting to run this script:

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

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

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.

quantumbutterfly
  • 1,815
  • 4
  • 23
  • 38

7 Answers7

20

you need to download the DRIVER

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

Community
  • 1
  • 1
Leon Barkan
  • 2,676
  • 2
  • 19
  • 43
  • 6
    On Windows it should look like `session = webdriver.PhantomJS(executable_path='C:\\Phantomjs\\bin\\phantomjs')` where last 'phantomjs' stands for phantomjs.exe – XXN Mar 30 '18 at 21:23
  • 2
    After working on PhantomJS for hours to find out what's wrong with it the last error I've got was "Selenium support for PhantomJS has been deprecated". Learn from me and don't waste your valuable time on this module. – Daniel Oct 26 '20 at 13:52
15

Working Solution:

Assumming you are on windows - it is similar for linux

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

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)

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

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

Dung
  • 19,199
  • 9
  • 59
  • 54
10

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'.

Ourik gruzdev
  • 126
  • 1
  • 4
  • This command need to be added in ~/.bashrc. For instance: >> sudo nano ~/.bashrc; then after adding the above command we need to do: >> source ~/.bashrc – VARAT BOHARA May 09 '21 at 14:40
2

You need to provide the executable path.This is for linux or more precisely 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.

Nandesh
  • 4,437
  • 2
  • 20
  • 26
1

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

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

C:\Users\[user]\AppData\Local\Programs\Python\Python[version]\Scripts
# you can use it as following 
from selenium import webdriver
driver = webdriver.PhantomJS()
Ahmed Soliman
  • 1,662
  • 1
  • 11
  • 16
0

1.Download Phantomjs executable from https://phantomjs.org/download.html 2. 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"

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

Guru
  • 155
  • 2
  • 6
0

This will work perfectly.

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")
Jiya
  • 745
  • 8
  • 19