Skip to content
CodeKruPro

Browser Back and Forward Navigation

3/70

Video walkthrough

Watch on YouTube

Browser Back and Forward Navigation

Selenium can simulate browser back/forward buttons:

  • driver.navigate().back() - goes to previous page
  • driver.navigate().forward() - goes to next page
  • driver.navigate().refresh() - refreshes current page

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Navigate to https://testkru.com/Elements/Buttons
  3. Go back and print the title
  4. Go forward and print the title

Expected output:

After back: Text Fields
After forward: Buttons

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

  1. Go back one page through the Navigation object that driver.navigate() returns.
  2. Print the title after the label After back:.
  3. Go forward one page the same way.
  4. 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, so back() from the first page you loaded does not stay put or throw: it lands on that blank page, and getTitle() returns an empty string. Only from data:, itself does back() do nothing, again without an error, which can hide a broken test step.

  • Keeping elements across back() or refresh()

    Both rebuild the page. Any WebElement you saved earlier becomes stale, and using it throws StaleElementReferenceException.

  • Reading the title only once

    Calling getTitle() once and printing the saved String twice shows the same page both times. Read the title again after each back() and forward(), 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.

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.

Main.java
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();
    }
}