Can’t run PhantomJS in python via Selenium

I have been trying to run PhantomJS via selenium for past 3 days and have had no success.
So far i have tried installing PhantomJS via npm, building it from source, installing via apt-get and downloading prebuilt executable and placing it in /usr/bin/phantomjs.

Every time I was able to run this example script loadspeed.js :

var page = require('webpage').create(),
    system = require('system'),
    t, address;

if (system.args.length === 1) {
    console.log('Usage: loadspeed.js <some URL>');

t =;
address = system.args[1];, function (status) {
    if (status !== 'success') {
        console.log('FAIL to load the address');
    } else {
        t = - t;
        console.log('Loading time ' + t + ' msec');

and run it with ‘phantomjs test.js‘ and it worked just as it should.

but running PhantomJS via selenium in this small python script produces errors:

from selenium import webdriver
browser = webdriver.PhantomJS()


Traceback (most recent call last):
  File "", line 4, in <module>
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/", line 176, in get
    self.execute(Command.GET, {'url': url})
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/", line 162, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/", line 350, in execute
    return self._request(url, method=command_info[0], data=data)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/", line 382, in _request
    resp = self._conn.getresponse()
  File "/usr/lib/python2.7/", line 1045, in getresponse
  File "/usr/lib/python2.7/", line 409, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.7/", line 373, in _read_status
    raise BadStatusLine(line)
httplib.BadStatusLine: ''

Replacing second LOC with browser = webdriver.Firefox() works fine.

I am on Ubuntu 13.10 desktop and same error occurs on Ubuntu 13.04 aswell.

Python: 2.7
PhantomJS: 1.9.2

What am I doing wrong here?

Best answer

There seems to be some issue introduced in newer Selenium, see

I got a bit further using by using

pip install selenium==2.37

Avoids the stack trace above. Still having problems with driver.save_screenshot(‘foo.png’) resulting in an empty file though.