Python / Selenium / PhantomJS: "Unable to start phantomjs with ghostdriver."

15

3

I'm trying to get Selenium to use phantomjs on a Windows machine. My code compiles without any error:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import cookielib
import re
from splinter import Browser

driver = webdriver.PhantomJS('C:/Program Files (x86)/phantomjs-1.9.2-windows')

But every single time I run it I get the error

Traceback (most recent call last):
  File "E:/~PROJECT/disinfo/py/bs.py", line 8, in <module>
    driver = webdriver.PhantomJS('C:/Program Files (x86)/phantomjs-1.9.2-windows')
  File "C:\Python27\lib\site-packages\selenium\webdriver\phantomjs\webdriver.py", line 50, in __init__
    self.service.start()
  File "C:\Python27\lib\site-packages\selenium\webdriver\phantomjs\service.py", line 63, in start
    raise WebDriverException("Unable to start phantomjs with ghostdriver.", e)
selenium.common.exceptions.WebDriverException: Message: 'Unable to start phantomjs with ghostdriver.' ; Screenshot: available via screen 

I've been getting this error for hours. "Unable to start phantomjs with ghostdriver". The simplest examples online show installing selenium with PIP and then phantomjs with NodeJS NPM, which is how I did it. Selenium's location is in my PYTHONPATH as well. I frankly have no idea what the hell this thing wants from me. Any ideas?

Amalgovinus

Posted 2013-11-11T08:29:33.440

Reputation: 307

Same issues on basic code that once worked, thought I had broke my python structure at first. – Shane – 2013-11-12T08:10:32.837

1

Welp, it looks like phantomjs' dev has admitted that the python bindings are behind. https://github.com/detro/ghostdriver/issues/236 So much for using phantomjs. Wish I would've known that before I spent several ****ing hours trying to wrangle phantomjs to life.

– Amalgovinus – 2013-11-16T04:44:52.910

1Same feelings here. Why it is so important to learn code and not libraries unless they are maintained by a large community. You can never expect one guy to maintain his project especially if not making any money off of the hard work. – Shane – 2013-11-16T20:47:53.543

Answers

19

I ran into the same issue. I needed to provide the full path to the exe, as described in the documentation:

from selenium import webdriver
driver = webdriver.PhantomJS(executable_path='C:\phantomjs-1.9.2\phantomjs.exe')

John

Posted 2013-11-11T08:29:33.440

Reputation: 306

This did indeed allow it to dial out, hooray. Now the only problem is, every element I try to print, it just gives me a "selenium.webdriver.phantomjs.webdriver.WebDriver object at 0x0000000002F54668" or a unicode ID. I recommend lorien's Grab instead. https://bitbucket.org/lorien/grab/src/98cbbfa4d2d2a5d4207c1086e24b4b922380bbfc/grab/tools/russian.py?at=default

– Amalgovinus – 2013-12-07T23:20:52.183

true story!! wish I could upvote 5 times. – HelloW – 2013-12-20T13:40:57.390

Didn't work for me... I had to edit service.py as per DT_Lvhyy's answer. – Deep-B – 2014-02-17T20:47:47.340

It looks like for Ubuntu and Linux systems, ~ should be the actual path from root to the directory – Delos Chang – 2014-03-18T06:47:40.497

2

You need to modify ...\Python27\Lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriver\phantomjs\service.py

This google code page would help.

DT_Lvhyy

Posted 2013-11-11T08:29:33.440

Reputation: 21