Make Selenium Easy

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

Last updated on May 27th, 2020 at 07:18 pm

In this post we will see an important and confusing topic – Difference between get() and navigate() methods. In fact this is not at all confusing as people just do not try to observe behavior by writing some lines of code and going through official documents.

Let me point down some differences which you people generally see :-

  1. get() method holds or waits till page is loaded while navigate() does not.
  2. get() method does not retain any browsing history while navigate() does.

In fact both above points are incorrect.

First of all navigate() method can not be used directly to load a URL. WebDriver interface has a method “navigate()” which returns a type Navigation which is an inner interface in WebDriver interface. Navigation interface is an abstraction allowing the driver to access the browser’s history by explicit methods and to navigate to a given URL.

This Navigation interface consists of below methods :-

Referring official document of Selenium is always good to learn valid points. It clearly 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 POST operation, and the method will block until the load is complete. 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 POST operation, and the method will block until the load is complete. “

Note – You will see in above doc, Selenium developers have mentioned as “This is done using an HTTP GET” which is incorrect. It is actually a POST request. I have reported this typo. You can find that bug details here.

So hopefully you must have got the point that to() and get() are same. In fact to() calls get() method internally.

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

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

If still you are confused then some lines of codes will work for sure.

In below program, I am loading same website using get() and to() methods and keeping timer.

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:

Difference between get() and navigate():

  1. Return type of get() is void while return type of navigate() is a Navigation interface which allows you use other methods.
  2. Using navigate() you get direct methods of Navigation interface to access browsing history by using back() and forward() methods. You can navigate to a URL as well as you can refresh current window. Using get() it is not possible to go back and forward without passing URL explicitly.
  3. There is no difference in internal implementation of get() and to() as to() method calls get() method internally.

You can download/clone above sample project from here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related post here, all API manual and automation related posts here and find frequently asked Java Programs here.

Many other topics you can navigate through menu.

Table of Contents

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

7 thoughts on “Selenium Interview Question 3 – Difference Between get() and navigate() Methods of Selenium WebDriver

  1. Hi Amod, really worth material……………….could you please add page numbers or previous and next page arrows links in the site so that people can learn page-by-page.

  2. Hi Amod,
    Contents of your website is very good but it would be better if you put contents in a darker fonts as it would help some of us with a weaker eye sight.

    Thank you!

    1. Hi Deepak,

      Thanks for letting me know this. I have changed accessibility recommended font now.

      Thanks
      Amod

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

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

by Amod Mahajan time to read: 4 min
7
%d bloggers like this: