About this lesson
Browser back, forward and refresh navigation in Selenium Java
Learn Selenium JavaBeginnerGetting Started
How it works
The browser keeps a history list of the pages a session has visited. Selenium Java exposes it through driver.navigate(), which returns a Navigation object with three history methods: back(), forward() and refresh(). They act exactly like the toolbar buttons a user would press.
Each get() call adds an entry to that list, so after the two loads Text Fields sits one step behind Buttons, with the blank data:, start page one step further back. back() and forward() wait for the target page to load before your next line runs, just like get().
About the assignment
Both pages are already loaded for you: Text Fields first, Buttons second. That leaves a two-entry history, and the assignment is to walk it: one step back to Text Fields, then one step forward to Buttons, reading the title at each stop.
What you will do
- Go back one page through the
Navigationobject thatdriver.navigate()returns. - Print the title after the label
After back:. - Go forward one page the same way.
- Print the title again, this time after the label
After forward:.
Common mistakes
Going back past the first page
A session starts on the blank
data:,page, soback()from the first page you loaded does not stay put or throw: it lands on that blank page, andgetTitle()returns an empty string. Only fromdata:,itself doesback()do nothing, again without an error, which can hide a broken test step.Keeping elements across back() or refresh()
Both rebuild the page. Any
WebElementyou saved earlier becomes stale, and using it throwsStaleElementReferenceException.Reading the title only once
Calling
getTitle()once and printing the savedStringtwice shows the same page both times. Read the title again after eachback()andforward(), because each move loads a different document.
In a real test suite
History methods are useful for checking that an application copes with users who press Back: a wizard that keeps earlier answers, a search page that restores its filters, or a single page app whose router updates the address correctly.
So far you have only read page-level facts such as the title. Lesson 4 starts on the elements inside a page, beginning with the most direct locator there is: the id attribute.
Related lessons
- Navigate Between PagesCreates the two history entries this lesson walks through.
- Handle Multiple WindowsTabs have separate histories; switching tabs is a different call from going back.
- Switch Between Frames and Default ContentLeaving an iframe uses
switchTo(), notback(), because a frame is not a history entry.
To try one of these calls on any page without a task, open the Online Selenium Java Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main(String[] args) throws Exception {
WebDriver driver = new ChromeDriver();
driver.get("https://testkru.com/Elements/TextFields");
driver.get("https://testkru.com/Elements/Buttons");
// TODO: Navigate back and print title
// TODO: Navigate forward and print title
driver.quit();
}
}