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

匆匆过客 提交于 2019-12-20 10:29:19

问题


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?


回答1:


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



回答2:


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

sudo apt-get install nodejs-legacy



回答3:


For me, this was a firewall issue. Phantom requires an open port to connect. If the port is blocked by a firewall, you'll get WebDriverException("Can not connect to GhostDriver").

To fix:

  1. Open a port.

sudo iptables -A INPUT -s 127.0.0.1 -p tcp --dport 65000 -j ACCEPT

  1. Create a PhantomJS driver that uses that port

driver = webdriver.PhantomJS(executable_path='/usr/local/bin/phantomjs', port=65000)




回答4:


I had this problem and found out what was causing it. In another tutorial on developing Facebook applications, I was told to nano in to /etc/hosts and change it from this-

127.0.0.1    localhost

to this-

127.0.0.1    test1.com

and save the file. Incidentally, PhantomJS() was not working anymore after that. I just went back in to the /etc/hosts file and switched it back to localhost

127.0.0.1    localhost

and it works again.




回答5:


I am testing a python/django application using selenium with phantomjs. Everything worked fine during first (and sometimes second test), but once running the next test, selenium printed the exact same error message.

Furthermore, if I ran phantomjs from console afterwards, I received the following node error:

child_process.js:1136 var err = this._handle.spawn(options); 
                                             ^

TypeError: Bad argument 
       at TypeError (native)
       at ChildProcess.spawn (child_process.js:1136:26)
       at exports.spawn (child_process.js:995:9)
       at Object.<anonymous> (/usr/local/lib/node_modules/phantomjs/bin/phantomjs:22:10)
       at Module._compile (module.js:460:26)
       at Object.Module._extensions..js (module.js:478:10)
       at Module.load (module.js:355:320)
       at Function.Module._load (module.js:310:12)
       at Function.Module.runMain (module.js:501:10)
       at startup (node.js:129:16)

I tried reinstalling phantomjs via npm (both locally and globally) several times, but the behaviour persisted.

Eventually, I uninstalled phantomjs via npm and downloaded the phantomjs bin from phantomjs.org instead. Put that inside my /usr/local/bin (I am on MacOS) and got rid of the error since!



来源:https://stackoverflow.com/questions/18916123/selenium-common-exceptions-webdriverexception-message-can-not-connect-to-ghos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!