Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests
To navigate back and forward in Selenium using Java, you utilize the ‘navigate‘ interface of the ‘WebDriver‘ class. Here’s a detailed description and examples of how to do this:
Here’s a step-by-step example demonstrating how to navigate back and forward in a browser using Selenium in Java:
				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class NavigationExample {
    public static void main(String[] args) {
        // Set the path to the chromedriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        
        // Initialize a new ChromeDriver instance
        WebDriver driver = new ChromeDriver();
        
        try {
            // Open the first URL
            driver.get("https://www.example.com");
            System.out.println("Opened: " + driver.getCurrentUrl());
            
            // Open the second URL
            driver.get("https://www.anotherexample.com");
            System.out.println("Opened: " + driver.getCurrentUrl());
            
            // Navigate back to the first URL
            driver.navigate().back();
            System.out.println("Navigated back to: " + driver.getCurrentUrl());
            
            // Navigate forward to the second URL
            driver.navigate().forward();
            System.out.println("Navigated forward to: " + driver.getCurrentUrl());
            
        } finally {
            // Close the browser
            driver.quit();
        }
    }
}

				
			

Explanation

  1. Set up the WebDriver path: This is necessary for Selenium to locate the browser driver executable (e.g., ‘chromedriver‘).
				
					System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

				
			

2. Initialize the WebDriver: Here we create an instance of the ChromeDriver which opens a Chrome browser.

				
					WebDriver driver = new ChromeDriver();

				
			

3. Navigate to URLs: Use the get method to open a web page.

				
					driver.get("https://www.example.com");
driver.get("https://www.anotherexample.com");

				
			

4. Navigate Back: Use the navigate().back() method to go back to the previous page.

				
					driver.navigate().back();

				
			

5. Navigate Forward: Use the navigate().forward() method to go forward to the next page.

				
					driver.navigate().forward();

				
			

6. Close the Browser: Finally, close the browser using ‘quit()‘ method to clean up the resources.

				
					driver.quit();

				
			

Additional Navigation Methods

  • Refresh the page: To refresh the current page, use the refresh() method.
				
					driver.navigate().refresh();

				
			
  • Navigate to a specific URL: Use the to() method which is another way to navigate to a URL.
				
					driver.navigate().to("https://www.example.com");

				
			

Complete Example with Additional Navigation Methods

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

public class NavigationExample {
    public static void main(String[] args) {
        // Set the path to the chromedriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        
        // Initialize a new ChromeDriver instance
        WebDriver driver = new ChromeDriver();
        
        try {
            // Open the first URL
            driver.get("https://www.example.com");
            System.out.println("Opened: " + driver.getCurrentUrl());
            
            // Open the second URL
            driver.get("https://www.anotherexample.com");
            System.out.println("Opened: " + driver.getCurrentUrl());
            
            // Navigate back to the first URL
            driver.navigate().back();
            System.out.println("Navigated back to: " + driver.getCurrentUrl());
            
            // Navigate forward to the second URL
            driver.navigate().forward();
            System.out.println("Navigated forward to: " + driver.getCurrentUrl());
            
            // Refresh the current page
            driver.navigate().refresh();
            System.out.println("Refreshed: " + driver.getCurrentUrl());
            
            // Navigate to a specific URL using navigate().to()
            driver.navigate().to("https://www.thirdexample.com");
            System.out.println("Navigated to: " + driver.getCurrentUrl());
            
        } finally {
            // Close the browser
            driver.quit();
        }
    }
}

				
			

This example shows how to use ‘navigate().back()‘, ‘navigate().forward()‘, ‘navigate().refresh()‘, and ‘navigate().to()' methods, providing a comprehensive overview of Selenium navigation in Java.

Scroll to Top