credit: seleniumhq.org |
TL;DR
In this post I will explain the cause of selenium webdriver unable to connect issue and how to resolve this issue.
In order to understand this issue, you first have to understand selenium webdriver (also known as Selenium 2) architecture. Selenium webdriver is a http server that uses not standard 80 port. I will provide example in Ruby and watir-webdriver library. Watir webdriver library uses selenium-webdriver in order to create more intuitive api for browser manipulation.
Run this code in Ruby irb. You will first need to install watir-webdriver gem:
gem install ‘watir-webdriver’
Now in your unix terminal, run:
netstat -a | grep localhost
and you will see something like this:
selenium-webdriver server is ready on your local machine on port 7055. So if you have anything that already runs on that port, selenium-webdriver will fail to start.
Your selenium-webdriver tests, written in any language in which library is provided, are client to selenium-webdriver tests. So, there is possibility that in your console you see something like this:
unable to connect to phantomjs @ http://127.0.0.1:7055 after 20 seconds.
If you see that error, you need to check if you run on your local machine any firewall software. If you are working in corporate environment, you are probably not aware that you are running firewall. Firewall default settings is to not allow any connections on localhost, because this is the usual way how malicious software operates. But in the case of selenium-webdriver, we have a false positive, selenium-webdriver is not malicious software.
DO NOT TURN OFF YOUR FIREWALL! Often, this is not possible. You need to add firewall exception for port 7055. Here is how to do that for Symantec endpoint protection:
In windows tray, right click on Endpoint protection icon and choose Open Symantec Endpoint Protection.
Choose yes.
Select Change settings.
Select Exceptions configure settings.
Add Security Risk Exceptions, Web Domain
Enter 127.0.0.1 and click ok.
Close exceptions window.
Close settings window.
Run your selenium-webdriver script in order to check that issue is gone.