Tips & Tricks

Selenium/ WebDriver Tips and Tricks


How to determine whether the webpage has been loaded or not ?

public static void waitForPageToLoad(WebDriver driver, int timeOutInSeconds){ 
JavascriptExecutor js = (JavascriptExecutor)driver; 
String command = "return document.readyState"; 

//Check the readyState before doing any waits 
if (js.executeScript(command).toString().equals("complete")){ 

for (int i=0; i<timeOutInSeconds; i++){ 
try {Thread.sleep(1000);} catch (InterruptedException e) {} 

if (js.executeScript(command).toString().equals("complete")){ 

What causes the org.openqa.selenium.remote.UnreachableBrowserException: ?

We might see sometimes these errors occur during our selenium test execution, what really causes these exceptions ?

Indicates there was a problem communicating with the browser being controlled or the Selenium server. The most common causes for this exception are:

  • The provided server address to RemoteWebDriver is invalid, so the connection could not be established.
  • The browser has died mid-test.
  • How to increase 45 sec waiting period to bind locking port 7054?

    During parallel execution of tests on FireFox you may encounter this error –

    //unable to bind to locking port 7054 within 45 seconds 
    FirefoxBinary fb = new FirefoxBinary();
    WebDriver driver = new FirefoxDriver(fb, null); // or pass in a FirefoxProfile you want instead of the default webdriver creates.

    Selenium RC

    How to handle Selenium is already running on port 4444 ?

    This is one of the common problems that we face while automate webapps using Selenium RC(Selenium 1.0).

    Failed to start: SocketListener0@
    Exception in thread "main" Selenium is already running on port 4444. Or some other service is.
    	at org.openqa.selenium.server.SeleniumServer.start(
    	at org.openqa.selenium.server.SeleniumServer.boot(
    	at org.openqa.selenium.server.SeleniumServer.main(

    You can very well kill this session and start freshly by using the command below in the browser.

    After you load this url the selenium sever that is running on port 4444 will be killed and you will notice a OKOK message in the browser.

    How to start Selenium Server Automatically using Java code ?

    Starting selenium sever manually in a Terminal(mac) or command prompt (windows) is a must do task before you kickoff your tests and we some times forget to do that and feel embarrassed (mainly in demos :P)

    This is method of automatically starting and shutting down of selenium server through Java code comes handy when the selenium RC scripts needs to be integrated with CI like Jenkins or Travis.

    Starting selenium server

    public static void startSeleniumServer(SeleniumServer server) throws Exception {
            try {
     	    ServerSocket serverSocket = new ServerSocket(RemoteControlConfiguration.DEFAULT_PORT);
     	            try {
     	             RemoteControlConfiguration rcc = new RemoteControlConfiguration();
     	             server = new SeleniumServer(false, rcc);
     	            } catch (Exception e) {
     	                System.err.println("Could not create Selenium Server because of: "
     	                        + e.getMessage());
     	            try {
     	            	System.out.println("Server started");
     	            } catch (Exception e) {
     	                System.err.println("Could not start Selenium Server because of: "
     	                        + e.getMessage());
     	        } catch (BindException e) {
     	            System.out.println("Selenium server already up, will reuse...");

    Stopping Selenium Server

    public static void stopSeleniumServer(SeleniumServer server, DefaultSelenium selenium){
     		if (server != null)
     	            server = null;
     	         catch (Exception e)


    Hope that helps!!

    3 thoughts on “Tips & Tricks

    1. Kris
      August 8, 2013 at 10:21 am

      Wow , dude, It’s really cool!!
      Thx a lot!

    2. SP
      August 20, 2013 at 11:16 pm

      I have two hudson jobs which run the selenium tests. One scheduled every hour and another one scheduled every half hour. We often see the “Selenium is already running on port 4444. Or some other service is.” error when the one of the job tries to run while the other job is already running. How do we overcome this ?

    3. Naveen
      May 8, 2014 at 12:24 pm

      I Have a small doubt about handling Unable to bind to locking port 7054.

      1. What could be the reasons for this issue. I am facing this frequently with FF28 and 2.41 server.
      2. You have given to simple code to increase time for FirefoxDriver, but what about Remote Webdriver. How should I pass this firefox binary object.


    Leave a Reply

    Your email address will not be published. Required fields are marked *