In this short article, we’ll be focusing on various Navigation commands that allow the browser to move forwards, backwards in the browser’s history.

2.Difference Between the driver.get(“url”) and driver.navigate().to(“url”);

On the basis of Method Calls:

driver.get() used to launch a particular website, it doesn’t maintain the browser History and cookies so, we can’t use forward and backward button whereas driver.navigate().to() is also used to launch the particular website but it maintains the browser history and cookies, so we can use forward and backward button to navigate between the pages during test case writing

On the grounds of page Load Time:

driver.get() wait till page gets loaded and perform actions thereafter ,considering  driver.navigate().to() does not wait for page load.

On account of Server API Calls:

driver.get() method internally sends HTTP GET request to Selenium Server Standalone. On the contrary driver.navigate().to method sends HTTP POST request to Selenium Server Standalone.

GET /session/:sessionId/url Retrieve the URL of the current page.
POST /session/:sessionId/url Navigate to a new URL.

On the grounds of page Load Time:

driver.get() is slow as it waits until the page is loaded.

driver.navigate().to() – faster as it does not wait for page to be loaded

3. Method Explanation

Now, to access the navigation methods, just type driver.navigate(). like below:

Let’s try to understand these methods in detail:

  1. Navigate To Command – driver.navigate().to(“url”)

There are two ways to define this method:

This method Loads a new web page in the current browser window. It accepts a String parameter and returns void.

This method accepts URL class instance by importing package import java.net.URL; and returns void.

2)     Forward Command – driver.navigate().forward();

This method enables the web browser to click on the forward button in the existing browser window, it takes you forward by one page on the browser’s history.  It neither accepts nor returns anything.

3) Back Commanddriver.navigate().back();

This method enables the web browser to click on the back button in the existing browser window. it takes you backwards by one page on the browser’s history. It neither accepts nor returns anything.

4)  Refresh Command- driver.navigate().refresh();

     This method refreshes the current page. It neither accepts nor returns anything.


Let’s look at this function in more details with below code snippet:

Upon executing the above code, we will get the output on console similar to:

Now many of us would be wondering why this exception occurs?

Let’s break down our answer to get a deeper understanding:

  • When WebDriver executes the above code then it assigns an internal id  to every WebElement and it refers this id to interact with that element and now we are refreshing our web Brower with driver.navigate().refresh();

  • It refreshes the entire page due to which the section of the DOM also gets refreshed and so is the element. In this scenario the internal id(something like “id =725541f2-6c3b-4a31-acd5-7c14ef0f8f97” as shown in below snapshot ) which WebDriver was using has become stale(old), so now for every operation on this WebElement, we will get StaleElementReferenceException .

To overcome this problem, the most recommended way is to reinitialize the element again and this time WebDriver will assign a different Id to this element, and example for this has been done for you below :

Let’s see complete code with all the navigate operations together:

package com.example.automation;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

@author Mandeep Kaur
@Date 23 April,2020

public class SeleniumConfig {
public static void main(String[] args) {

String path = System.getProperty(“user.dir”);
System.setProperty(“webdriver.chrome.silentOutput”, “true”);
System.setProperty(“webdriver.chrome.driver”, path + “/chromedriver”);
WebDriver driver = new ChromeDriver();

//Navigate to the google

//Navigate to the yahoo

//Navigate back to google

//Navigate back to yahoo

//Navigate to the instagram

//Refresh the page

//close the driver


4. Conclusion

In this article, we learnt about various selenium navigation commands.

Blog Contributors:

Author:  Mandeep Kaur
Mandeep, having 5+ years of Testing experience in automation using Selenium (Java). Expertise in API and Performance testing using JMeter.
Reviewer: Naveen Khunteta 

Please follow and like us: