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?
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:
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!
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-
to this-
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
and it works again.
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:
sudo iptables -A INPUT -s 127.0.0.1 -p tcp --dport 65000 -j ACCEPT
driver = webdriver.PhantomJS(executable_path='/usr/local/bin/phantomjs', port=65000)
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 tonode
.These are the commands I used:
Installing nodejs-legacy package on Linux Mint 14 solved this for me.