Browser Navigation Commands in Selenium
Selenium WebDriver provides methods to navigate through browser history, refresh pages, and open URLs. These commands help simulate user actions like going back, forward, or reloading a page.
Command | Syntax | Purpose |
Alternative URL Loading | driver.navigate().to(“url”); | Same as get(), but allows chaining with other navigation commands. |
Go Back | driver.navigate().back(); | Navigates to the previous page in browser history. |
Go Forward | driver.navigate().forward(); | Navigates to the next page in browser history (if available). |
Refresh Page | driver.navigate().refresh(); | Reloads the current webpage. |
Sample Program
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class BrowserNavigationDemo {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
// 1. Load initial page (Google)
driver.get("https://www.google.com");
System.out.println("Loaded: " + driver.getTitle());
// 2. Navigate to Amazon (alternative to get())
driver.navigate().to("https://www.amazon.com");
System.out.println("Navigated to: " + driver.getTitle());
// 3. Go back to Google
driver.navigate().back();
System.out.println("Went back to: " + driver.getTitle());
// 4. Go forward to Amazon
driver.navigate().forward();
System.out.println("Went forward to: " + driver.getTitle());
// 5. Refresh current page
driver.navigate().refresh();
System.out.println("Refreshed: " + driver.getTitle());
// 6. Maximize window (Bonus)
driver.manage().window().maximize();
// Close the browser
driver.quit();
}
}
How to do Title and Current URL Validation in Selenium ?
1. getTitle() Method
Purpose: Fetches the title of the current webpage.
What it returns:
- A String containing the text between the HTML <title> tags.
- Example: If the page title is “Google”, driver.getTitle() returns “Google”.
Common Uses:
- Verify if the browser landed on the expected page.
- Log the page title for debugging.
Conditional logic (e.g., proceed only if the title matches).
2. getCurrentUrl() Method
Purpose: Fetches the full URL of the current webpage in the browser’s address bar.
What it returns:
- A String with the complete URL (e.g., “https://www.google.com/search?q=selenium”).
Common Uses:
- Confirm navigation to the correct URL (e.g., after clicking a link).
- Check for redirects or URL parameters.
- Validate secure connections (e.g., https://).
Sample Example:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ValidationDemo {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
// Navigate to page
driver.get("https://www.google.com");
// Title Validation
String actualTitle = driver.getTitle();
if (actualTitle.equals("Google")) {
System.out.println("✓ Title correct: " + actualTitle);
} else {
System.out.println("✗ Title mismatch. Expected 'Google', Got: " + actualTitle);
}
// URL Validation
String currentUrl = driver.getCurrentUrl();
if (currentUrl.startsWith("https://www.google.")) {
System.out.println("✓ Valid Google URL: " + currentUrl);
} else {
System.out.println("✗ Invalid URL: " + currentUrl);
}
}
Note: For titles/URLs that change (e.g., timestamps), use contains() or regex instead of exact matches.