Navigation Commands

Back, Forward, Refresh

driver.get("https://shopease.example.com");
driver.navigate().to("https://shopease.example.com/products/101");
driver.navigate().back();      // home page
driver.navigate().forward();   // product 101 again
driver.navigate().refresh();   // reload, like F5
System.out.println(driver.getCurrentUrl());

Example

Output:

Run NavigationTest.main()

Example output

https://shopease.example.com/products/101

Simulating Browser History

Example

Browsers keep two stacks: back and forward. This plain-Java model shows why back() then forward() returns to the same page:

import java.util.*;

public class Main {
    static Deque<String> backStack = new ArrayDeque<>();
    static Deque<String> forwardStack = new ArrayDeque<>();
    static String current = "about:blank";

    static void to(String url) { backStack.push(current); current = url; forwardStack.clear(); }
    static void back() { forwardStack.push(current); current = backStack.pop(); }
    static void forward() { backStack.push(current); current = forwardStack.pop(); }

    public static void main(String[] args) {
        to("/home");
        to("/products/101");
        to("/cart");
        back();    System.out.println("after back:    " + current);
        back();    System.out.println("after back:    " + current);
        forward(); System.out.println("after forward: " + current);
    }
}

Output

after back:    /products/101
after back:    /home
after forward: /products/101