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

symbiotech picture symbiotech · Sep 20, 2013 · Viewed 16.6k times · Source

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:

try:
    socket_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socket_.settimeout(1)
    socket_.connect(("localhost", port))
    socket_.close()
    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(
            port=29842,
            desired_capabilities={
                'javascriptEnabled': True,
                'platform': 'windows',
                'browserName': 'Mozilla',
                'version': '5.0',
                'phantomjs.page.settings.userAgent': "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:

    try:
        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
        time.sleep(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?

Answer

user2356431 picture user2356431 · Nov 17, 2013

I had this issue on Ubuntu after upgrading to a new version. I re-installed all of the nodejs and python packages, but what I think solved the issue was making sure the nodejs executable was symbolically linked to node.

These are the commands I used:

apt-get remove node nodejs
apt-get install build-essential python-dev phantomjs npm nodejs
ln -s /usr/bin/nodejs /usr/bin/node
npm install -g phantomjs
pip install selenium bson BeautifulSoup pymongo