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

♀尐吖头ヾ 提交于 2019-12-02 22:44:34

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

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

sudo apt-get install nodejs-legacy

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)

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.

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!

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