Selenium WebDriver - Navigation Commands
Last Updated :
28 Apr, 2025
Selenium WebDriver is quite a popular open-source framework used for automating web browsers. It helps the developers to automate web-based operations and interactions by creating code in Java, Python, C#, and others. When automating tests, Selenium WebDriver offers a set of navigation commands that let you interact with and manage a web browser's navigation. In this article, we will learn about the navigation commands in detail.
What are Navigation Commands?
Navigation commands in Selenium WebDriver perform operations that involve navigating through web pages and controlling browser behavior. These commands provide an efficient way to manage a browser's history and perform actions like going back and forward between pages and refreshing the current page.
The Navigation Command provides four methods: to(), back(), forward(), and refresh(). These methods allow the WebDriver to perform the following operations:
1. to() Command
Loads a new web page in the current browser window. It accepts a string parameter that specifies the URL of the web page to be loaded.
2. back() Command
Moves back one step in the browser’s history stack. It does not accept any parameters and does not return anything.
3. forward() Command
Moves forward one step in the browser’s history stack. It does not accept any parameters and does not return anything.
4. refresh() Command
Reloads the current web page in the browser window. It does not accept any parameters and does not return anything.

Prerequisites
1. Java Development Kit (JDK): Install the JDK to write and execute Java code.
2. Integrated Development Environment (IDE): Choose an IDE like Eclipse, IntelliJ IDEA, or Visual Studio Code for writing and managing your Java projects.
3. Selenium WebDriver Java Client JAR Files: These files enable you to interact with the Selenium WebDriver API using Java.
4. WebDriver Executable: Download the WebDriver executable for your preferred browser (e.g., ChromeDriver, GeckoDriver) to automate browser actions.
5. Dependencies: Configure the required dependencies in your Java project to use Selenium WebDriver.
6. Import the required packages.
org.openqa.selenium.WebDriver
org.openqa.selenium.chrome.ChromeDriver
7. Set the system property for ChromeDriver (path to chromedriver executable).
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
8. Create the instance of the ChormeDriver.
WebDriver driver = new ChromeDriver();
For detailed installation steps, refer to this article.
Navigation Commands
1. Navigate to() command
Method:
driver.get("https://www.geeksforgeeks.org/");
OR
driver.navigate().to("https://www.geeksforgeeks.org/");
Output:

Explanation:
- This method loads a new web page in the current browser window.
- It accepts a string parameter that specifies the URL of the web page to be loaded.
- This method is equivalent to the driver.get() method, which also loads a new web page in the current browser window.
- However, the driver.get() method waits for the page to load completely before returning control to the script, while the driver.navigate().to() method does not wait for the page to load and returns immediately.
Therefore, the driver.navigate().to() method is faster than the driver.get() method, but it may cause synchronization issues if the script tries to interact with elements that are not yet loaded on the page.
2. back() Command
Method:
driver.navigate().back();
Output:

Explanation:
- This method moves back one step in the browser’s history stack.
- It does not accept any parameters and does not return anything.
- This method is equivalent to clicking on the back button of the browser.
- It can be used to return to the previous web page that was visited in the current browser session.
3. forward() Command
Method:
driver.navigate().forward();
Output:

Explanation:
- This method moves forward one step in the browser’s history stack.
- It does not accept any parameters and does not return anything.
- This method is equivalent to clicking on the forward button of the browser.
- It can be used to go to the next web page that was visited in the current browser session after using the back() method.
4. refresh() Command
Method:
driver.navigate().refresh();
Output:

Explanation:
- This method reloads the current web page in the browser window.
- It does not accept any parameters and does not return anything.
- This method is equivalent to clicking on the refresh button of the browser or pressing F5 key on the keyboard.
- It can be used to refresh the content of the web page or to retry a failed operation.
Implementation
Interaction with GeeksForGeeks Website using Navigation Commands:
- It starts by initializing the ChromeDriver and navigating to the GFG Home page.
- After waiting for 3 seconds, it clicks on the "Data Structures" link on the GeeksforGeeks website, which redirects to another web page.
- The program then waits for another 3 seconds before using the navigate().back() command to go back to the previous page (Home page of GeeksforGeeks).
- After waiting for 3 seconds, the navigate().forward() command is used to navigate forward to the "Data Structures" page again.
- The program waits for another 3 seconds and then uses the navigate().refresh() command to refresh the current web page.
- Lastly, the program closes the browser using the driver.close() command and displays "Browser closed" in the console.
Below is the Java program to implement the above approach:
Java
// Java program to implement
// navigation commands
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class NavigationDemo {
public static void main(String[] args)
throws InterruptedException
{
// Set the system property for
// ChromeDriver (path to chromedriver
// executable)
System.setProperty(
"webdriver.chrome.driver",
"D:/chromedriver_win32/chromedriver.exe");
// Initialize the WebDriver for Chrome browser
WebDriver driver = new ChromeDriver();
// Maximize the browser window
driver.manage().window().maximize();
driver.navigate().to(
"https://www.geeksforgeeks.org/");
System.out.println("Visited GeeksForGeeks");
// Waiting for 3 seconds
Thread.sleep(3000);
// Clicking on a link that redirects to another web
// page
driver.findElement(By.linkText("Data Structures"))
.click();
System.out.println(
"Clicked on Data Structures link present on navigation");
// Waiting for 3 seconds
Thread.sleep(3000);
// Using navigate.back() command to go back to the
// previous web page
driver.navigate().back();
System.out.println(
"Back to Home page of GeeksForGeeks");
// Waiting for 3 seconds
Thread.sleep(3000);
// Using navigate.forward() command to go forward to
// the next web page
driver.navigate().forward();
System.out.println(
"Navigated forward to the Data Structures page");
// Waiting for 3 seconds
Thread.sleep(3000);
// Using navigate.refresh() command to
// refresh the current web page
driver.navigate().refresh();
System.out.println(
"Refreshed the current web page");
// Waiting for 3 seconds
Thread.sleep(3000);
// Closing the browser
driver.close();
System.out.println("Browser closed");
}
}
Output:

Conclusion
Navigation commands available in Selenium WebDriver, including to(), back(), forward(), and refresh() are crucial for controlling the browser's history and movements while automating web tasks, ensuring accurate and dependable web testing results.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read