Selenium Interview Question 3 – Difference Between get() and navigate() Methods of Selenium WebDriver

This is the mostly asked interview question and confusing too for Selenium testers. People generally differentiate get() and navigate() methods as below:

get() method holds or waits till page is loaded while navigate() does not.

Above statement is NOT CORRECT.

Referring official document of Selenium is always good to learn valid points. It says get() method is synonyms for to() method. It makes sense that both methods do the same thing.

It defines get() as :

Load a new web page in the current browser window. This is done using an HTTP GET operation, and the method will block until the load is complete.  It is synonym for WebDriver.Navigation.to(String). “.

Let’s see how they define to() method:

Load a new web page in the current browser window. This is done using an HTTP GET operation, and the method will block until the load is complete.

Now I will show you the implementation of methods as well to confirm above behavior:

Go to RemoteWebDriver class of Selenium WebDriver and check implementation of get() method:



In the same class, there is an inner class called “RemoteNavigation”. This class defines methods to(), refresh(), back() and forward(). Check implementation of to().

You can see that to() method internally calls get() method itself. Apply inner class concept of Java, if you have doubt that get() method in to() belongs to outer class named “RemoteWebDriver”.

Let’s see above points practically as well:

Java Program:

Output:

You can see get() and to() both waited till page is loaded. But I am sad that my website loads slow. 🙁

Then question is still up that what is actual difference between get() and navigate(). Answer comes from official document again.

Since navigate has access on browser’s history, you can navigate back and forward as we do in a browser manually by clicking back and forward button. It also provides you to refresh the currently loaded page.

There is another interesting catch here. If you understand here that if a load a URL using to() method then only you can navigate back and forward, its NOT correct. I see people say that get() does not keep history while navigate() does. It is not like that.

Every URL loaded in browser will be in browser history and navigate allows to access that history. So below code will work absolutely fine:

A very long posts but worth if you people find it. Let me know your thoughts in comments.

#HappySelenium

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning