Navigation helps you move between web pages during testing. It lets you go back, forward, or refresh pages like a real user.
Navigation (back, forward, refresh) in Selenium Java
driver.navigate().back(); driver.navigate().forward(); driver.navigate().refresh();
Use driver.navigate() to access navigation methods.
These methods simulate browser buttons: back, forward, and refresh.
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
This test opens two pages, goes back to the first, then forward to the second, and refreshes the page. It prints the current URL after back and forward navigation.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class NavigationTest { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); // Open first page driver.get("https://example.com/page1"); Thread.sleep(1000); // wait 1 second // Open second page driver.get("https://example.com/page2"); Thread.sleep(1000); // wait 1 second // Go back to first page driver.navigate().back(); Thread.sleep(1000); System.out.println("Back URL: " + driver.getCurrentUrl()); // Go forward to second page driver.navigate().forward(); Thread.sleep(1000); System.out.println("Forward URL: " + driver.getCurrentUrl()); // Refresh the current page driver.navigate().refresh(); System.out.println("Page refreshed."); driver.quit(); } }
Always wait a little after navigation to let the page load before interacting.
Use driver.getCurrentUrl() to check which page is loaded after navigation.
Remember to close the browser with driver.quit() to free resources.
Navigation methods let you move back, forward, or refresh pages like a user.
Use driver.navigate() with back(), forward(), and refresh().
Check the current page URL to confirm navigation worked as expected.
Practice
driver.navigate().back() method do in Selenium WebDriver?Solution
Step 1: Understand the back() method
Theback()method simulates clicking the browser's back button, moving to the previous page in history.Step 2: Compare with other navigation methods
forward()moves forward,refresh()reloads the page, and closing the browser is done bydriver.close().Final Answer:
It navigates the browser to the previous page in history. -> Option BQuick Check:
back() = previous page [OK]
- Confusing back() with refresh()
- Thinking back() closes the browser
- Mixing back() with forward()
Solution
Step 1: Recall the refresh method syntax
In Selenium Java, refreshing the page is done by callingdriver.navigate().refresh();.Step 2: Eliminate incorrect options
driver.refresh(),driver.navigate().reload(), anddriver.refreshPage()are not valid Selenium WebDriver methods.Final Answer:
driver.navigate().refresh(); -> Option AQuick Check:
refresh() method is under navigate() [OK]
- Using driver.refresh() directly
- Using non-existent reload() method
- Confusing refresh with back or forward
driver.get("https://example.com/page1");
driver.get("https://example.com/page2");
driver.navigate().back();
String currentUrl = driver.getCurrentUrl();What will be the value of
currentUrl after execution?Solution
Step 1: Trace navigation steps
The driver first opens page1, then page2. Callingnavigate().back()moves back to page1.Step 2: Check current URL after back()
After going back,getCurrentUrl()returns the URL of page1.Final Answer:
"https://example.com/page1" -> Option CQuick Check:
back() returns to previous URL [OK]
- Assuming current URL stays on page2
- Expecting an exception on back()
- Confusing back() with refresh()
driver.get("https://example.com");
driver.navigate().forward();
String url = driver.getCurrentUrl();What is the problem?
Solution
Step 1: Understand forward() behavior
Theforward()method moves forward in browser history only if there is a forward page available.Step 2: Analyze code context
Since noback()was called before, there is no forward page, soforward()does nothing.Final Answer:
Calling forward() without a previous back() navigation does nothing. -> Option DQuick Check:
forward() needs prior back() [OK]
- Expecting forward() to always change page
- Thinking forward() causes syntax error
- Confusing get() and navigate().to()
Solution
Step 1: Understand navigation flow
To test returning to the original page, first go back, then forward to return to the original page.Step 2: Verify correct command order
driver.navigate().back(); driver.navigate().forward(); String url = driver.getCurrentUrl(); correctly callsback()thenforward(), then gets the current URL.Final Answer:
driver.navigate().back(); driver.navigate().forward(); String url = driver.getCurrentUrl(); -> Option AQuick Check:
Back then forward returns to original URL [OK]
- Calling forward() before back()
- Using refresh() instead of forward()
- Not checking URL after navigation
Ready to prove it?
Pass the test and this pattern moves unknown
