Selenium WebDriver - Navigation Commands
Last Updated :
24 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
Selenium WebDriver Commands
Selenium WebDriver is a powerful tool for automating the web browser to perform certain tasks. Selenium supports multiple browsers (such as Chrome, Firefox, Edge, Safari, etc.) and multiple programming languages (such as Java, Python, C#, etc.) so, it is very easy to use and automate tasks on a brow
7 min read
Selenium WebDriver-Installation
Selenium WebDriver is a powerful tool for automating web applications for testing purposes. It allows developers and testers to write automated tests in various programming languages like Java, Python, C#, etc. Also, it supports different browsers like Firefox, Chrome, Edge, etc. for testing. Approa
2 min read
Selenium WebDriver - Browser Commands
Selenium WebDriver is an extremely useful tool for automating web applications and browser interactions. Through its collection of browser commands, developers and testers can control web browsers in a programmatic fashion. Browser commands that enable tasks such as web scraping, simplifying everyda
7 min read
Applications and Uses of Selenium WebDriver
Selenium Webdriver is a powerful tool for controlling web browser through program. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc. Selenium Webdriver is a primary automation tool used by developers all around the wo
3 min read
Limitations of Selenium Webdriver
Selenium is a powerful tool for controlling web browser through program. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc but it has some disadvantages and limitations such as it doesn't support Windows or Desktop app
2 min read
Firebug and FirePath in Selenium WebDriver
Firebug and FirePath are popular tools for web developers and testers, and they are specially designed for those working with Selenium WebDriver for automating web applications. They are primarily used with the Firefox browser to inspect and interact with web elements, aiding in the development and
7 min read
Selenium WebDriver - WebElement Commands
Selenium is an open-source program that automates web browsers. Selenium Webdriver is mainly used to execute the scripts according to the browser we are using. Prerequisites1. Install the Selenium Python Package.Use the following command to install the Selenium Python package: pip install selenium 2
6 min read
Selenium WebDriver-Handling Alerts
Selenium is one of the most popular and powerful tools for automated web applications. Selenium is widely used for automating user interactions like filling out forms, navigating to a website clicking on a button, etc. While we are dealing with automating user interactions one of the most common sce
5 min read
Selenium WebDriver Event Listener
Testing Websites often includes testing multiple pages in the website. "Selenium" is one of the most popular test automated frameworks used to test multiple web pages provides numerous functionalities and enables the interaction between web pages. The name "Listeners" suggests that they can listen t
5 min read
Features of Selenium WebDriver
Selenium is a powerful tool for controlling web browser through program. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C# etc, we will be working with Python. This article revolves around Major Features of Selenium WebDriv
2 min read