Navigate commands in Selenium WebDriver

Navigate in selenium: As we work with web pages, we may need to move forward or back of the pages to verify something or to analyze the page behavior. Navigate feature is used in selenium web driver to achieve these functionalities.

    • driver.navigate().back();
    • driver.navigate().forward();
    • driver.navigate().refresh();
    • driver.navigate().to(“”);

driver.navigate().back() – Used to move page backward by one level. It emulates the browsers Back button.

driver.navigate().forward() – Used to move page forward by one level.

driver.navigate().refresh() – Used for current page refresh.

driver.navigate().to(” “) – This method loads the given URL. Input parameter for this method is a URL in the form of String.



package com;

import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Navigate {

static WebDriver driver;
String homepageTitle = “”;
String nextTitle = “”;

public void before() throws Exception {
// chrome standalone server
driver = new ChromeDriver();

// implicit wait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

// this opens the url
homepageTitle = driver.getTitle();

public void navigate_selenium() throws Exception {
nextTitle = driver.getTitle();

Assert.assertTrue(“Page has moved backwards.”, homepageTitle.equals(driver.getTitle()));

Assert.assertTrue(“Page has moved forwards.”, nextTitle.equals(driver.getTitle()));

Assert.assertTrue(“Page has refreshed successfully.”, nextTitle.equals(driver.getTitle()));

public void tearDown() {


Output on the console:

PASSED: navigate_selenium

Explanation: In this script,

1. First we are navigating to homepage and storing the page title in a variable named homepageTitle.

2. Then navigating to a different page and storing the page title in a variable named nextTitle.

3. Moving backwards and verifying the title of the page by using an Assertion.

4. Moving forwards and verifying the title of the page by using an Assertion.

5. Refreshing the page and verifying the title of the page by using an Assertion.

Other selenium topics:

TestNG and annotations

How to handle mouse hover events using Selenium WebDriver

WebElement actions in selenium WebDriver

Top 25 selenium interview questions

How to take screenshot using Selenium WebDriver