selenium.common.exceptions.WebDriverException: Message: 'Can not connect to GhostDriver'

1 Answers

Installing nodejs-legacy package on Linux Mint 14 solved this for me.

sudo apt-get install nodejs-legacy

I'm trying to run PhantomJS from within selenium.webdriver on a Centos server. PhantomJS is in the path and is running properly from terminal. However in the script it appears to be launched, but afterwards cannot be reached on the specified port (I tried 2 different opened ports from my provider 29842 and 60099, they both are not working and neither launching it without a specified port).

The error happens here in selenium.webdriver.common.utils:

    socket_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socket_.connect(("localhost", port))
    return True
except socket.error:
    return False

This is from my script (I tried without any parameters as well as writing the complete path to the executable and neither worked):

self.browser = webdriver.PhantomJS(
                'javascriptEnabled': True,
                'platform': 'windows',
                'browserName': 'Mozilla',
                'version': '5.0',
                '': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36"

And this the script that initialises the webdriver from selenium.webdriver.phantomjs.service. I checked and subprocess.Popen actually lauches phantomjs, the error happens in the while loop:

        self.process = subprocess.Popen(self.service_args,
                                        stdout=self._log, stderr=self._log)

    except Exception as e:
        raise WebDriverException("Unable to start phantomjs with ghostdriver.", e)

    count = 0
    while not utils.is_connectable(self.port):
        print utils.is_connectable(self.port)
        count += 1
        if count == 30:
             raise WebDriverException("Can not connect to GhostDriver")

All the packages are the latest version: python 2.7, selenium 2 and phantomjs 1.9 binary with ghostdriver integrated. I made the same script work properly on my Ubuntu local machine, doing exactly the same things I did on the server. What is different on the server?

How to install PhantomJS for use with Python Selenium on the Raspberry Pi?

Ok I will start with the solution, there is a version compiled for arm here phantomjs-linux-armv6l, on the pi run the following commands:

$ cd /tmp
$ wget
$ unzip
$ cd phantomjs-linux-armv6l-master
$ bunzip2 *.bz2 && tar xf *.tar

I added:

sudo cp phantomjs-1.9.0-linux-armv6l/bin/phantomjs  /usr/bin

So phantomjs will be on your path.

pi@raspberrypi ~ $ phantomjs --version

pi@raspberrypi ~ $ phantomjs

Now we have that done, time to test:

pi@raspberrypi ~ $ cat
from selenium import webdriver

driver = webdriver.PhantomJS()
a = driver.find_element_by_xpath('//*[@id="question-header"]/h1/a')
pi@raspberrypi ~ $ python 
How to install PhantomJS for use with Python Selenium on the Raspberry Pi?
<selenium.webdriver.phantomjs.webdriver.WebDriver (session="b184e110-f9c4-11e5-aede-7f5c42f062d7")>

From the faq. Starting with PhantomJS 1.5, it is pure headless and there is no need to run X11/Xvfb anymore..

I tried using xvfb-run and exporting the display, using a shell script in init.d to start xvfb, I got a little further being able to run iceweasel from bash headless no problem but still no cigar when it came to phantomjs and selenium. I think it may just come down to an incompatibility between selenium and the version of phantomjs, regardless having 1.9.0 and real headless browsing is a lot more desirable.

I was in the middle of setting up a toolchain and was going to try to compile myself when I found the link above, for anyone interested in cross compiling, crosstools-ng makes life a lot easier.

I am running an arm6, there is also a compiled version for arm7 using 2.0.0, the dependencies are:

sudo apt-get install flex bison gperf ruby perl libsqlite3-dev libfontconfig1-dev libicu-dev libfreetype6 libssl-dev libpng-dev libjpeg-dev python libX11-dev libxext-dev

The install procedure, I have extracted the binary to dropbox:

chmod +x phantomjs
sudo cp phantomjs /usr/bin

The original github link is phantomjs-2.0.0-armv7