๐ Navigating Between Pages Using Selenium WebDriver (Java)
In web automation, you often need to move between pages — for example, clicking links, going back to a previous page, or refreshing the browser.
Selenium WebDriver provides several built-in navigation commands for this purpose.
๐งญ 1. The navigate() Method
Selenium provides a special navigation interface through the WebDriver.Navigation class, which can be accessed using:
driver.navigate()
This gives you access to methods for:
Opening URLs
Moving backward or forward in browser history
Refreshing pages
๐งฉ 2. Navigation Methods
Method Description
navigate().to(String url) Opens the given URL (similar to get() method).
navigate().back() Moves one step backward in browser history.
navigate().forward() Moves one step forward in browser history.
navigate().refresh() Reloads the current web page.
๐ป 3. Example: Navigating Between Pages
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class NavigationExample {
public static void main(String[] args) throws InterruptedException {
// Set up ChromeDriver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
// Open first website
driver.get("https://www.google.com");
System.out.println("Opened: " + driver.getTitle());
// Navigate to another website
driver.navigate().to("https://www.wikipedia.org");
System.out.println("Now on: " + driver.getTitle());
// Go back to the previous page
driver.navigate().back();
System.out.println("After Back: " + driver.getTitle());
// Go forward again
driver.navigate().forward();
System.out.println("After Forward: " + driver.getTitle());
// Refresh the page
driver.navigate().refresh();
System.out.println("Page refreshed!");
// Close the browser
driver.quit();
}
}
๐ง 4. Difference Between get() and navigate().to()
Method Description
driver.get("url") Loads a new web page and waits until it is fully loaded.
driver.navigate().to("url") Similar to get(), but allows moving back and forward in browser history later.
Both methods open a webpage, but navigate().to() is more flexible when doing multiple navigations.
⚙️ 5. Navigating Using Links
You can also navigate by clicking links instead of URLs.
Example:
driver.findElement(By.linkText("About")).click();
After clicking:
driver.navigate().back(); // Go back to the previous page
⏱️ 6. Adding Waits Between Navigations
Sometimes pages load dynamically, so it’s a good idea to pause between navigations:
Thread.sleep(2000); // Wait 2 seconds
Or, use Selenium’s WebDriverWait for more control:
new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.titleContains("Wikipedia"));
๐งฉ 7. Summary of Navigation Commands
Command Purpose
driver.get("URL") Opens a web page
driver.navigate().to("URL") Navigates to a web page
driver.navigate().back() Goes to the previous page
driver.navigate().forward() Moves to the next page
driver.navigate().refresh() Refreshes the current page
๐ Example Workflow
A real-world test flow might look like this:
Open your homepage
Click a “Contact Us” link
Go back to the homepage
Go forward to “Contact Us” again
Refresh the page
You can easily achieve all these steps using the navigation methods shown above.
Learn Selenium with JAVA Training in Hyderabad
Read More
How to Handle Input Forms in Selenium
Working with ChromeDriver, GeckoDriver, and EdgeDriver
Introduction to WebDriver Interface in Java
Difference Between Selenium RC, IDE, and WebDriver
Visit Our Quality Thought Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments