Navigation commands enable the user to work with a history of the browser like back, forth, refresh, to. We can access these methods by accessing the navigate() method present in the driver (FF, IE, GC, Safari) class; we can access these methods we can access through navigate() method are below:

  • to(String string)
  • to(Url url) //overloaded to() method
  • back()
  • forward()
  • refresh()
to(String value):

to(string) method navigates the user to the particular webpage

public class To
{
	public static void main(String[] args) throws Exception
	{
		WebDriver driver=new FirefoxDriver();
		driver.get("https://chercher.tech");
		driver.navigate().to("https://gmail.com");
	}
}
Elaborated code for to(String value):
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.firefox.FirefoxDriver;

public class To
{
	public static void main(String[] args) throws Exception
	{
		WebDriver driver=new FirefoxDriver();
		driver.get("https://chercher.tech");
		Navigation nav = driver.navigate();
		nav.to("https://gmail.com");
	}
}
Elaborated code for to(URL url):
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.firefox.FirefoxDriver;

public class To
{
	public static void main(String[] args) throws Exception
	{
		WebDriver driver=new FirefoxDriver();
		driver.get("https://selenium-mentor.com");
		driver.navigate().to(new URL(https://gmail.com));
	}
}
back():

back() method helps the user to navigate back.

Optimized code for navigate back()

public class Back
{
	public static void main(String[] args) throws Exception
	{
		WebDriver driver=new FirefoxDriver();
		driver.get("https://chercher.tech");
		driver.navigate().back();
	}
}

In the above program, you might not face any error, but the browser also will not navigate anywhere, why? Because back() calls the back() native method present in the browser indirectly, so back works only when you navigate more than one page.

Modify the above program like below one

public class Back
{
	public static void main(String[] args) throws Exception
	{
		WebDriver driver=new FirefoxDriver();
		driver.get("https://chercher.tech");
		driver.get("https://google.com");
		driver.navigate().back();
	}
}
forward():

The forward() method navigates the user to forth but if we want forward() method to work we should use back() before using forward() then only the forward button in the browser or it creates the place in the browser history Syntax:driver.navigate().forward();

public class Forward
{
	public static void main(String[] args) throws Exception
	{
		WebDriver driver=new FirefoxDriver();
		driver.get("https://chercher.tech");
		driver.get("https://google.com");
		driver.navigate().back();
		driver.navigate().forward();
	}
}
refresh():

refresh() method refreshes the current webpage not suggested for https pages

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Refresh
{
	public static void main(String[] args) throws Exception
	{
		WebDriver driver=new FirefoxDriver();
		driver.get("https://chercher.tech");
		driver.navigate().refresh();
	}
}

Different ways to refresh a page

1.Using sendKeys.Keys method

driver.get("https://chercher.tech");
driver.findElement(By.tageName("body")).sendKeys(Keys.F5);


2.Using navigate.refresh() method

driver.get("https://chercher.tech");
driver.navigate().refresh();


3.Using navigate.to() method

driver.get("https://chercher.tech");
driver.navigate().to(driver.getCurrentUrl());


4.Using get() method

driver.get("https://chercher.tech");
driver.get(driver.getCurrentUrl());


5.Using sendKeys() method using keyboard

driver.get("https://accounts.google.com/SignUp");
driver.findElement(By.id("firstname-placeholder")).sendKeys("uE035");
About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • murali krishna
    thank you for the article which you posted with neat and clean explanation
    Reply
    • karthiQ [ admin]
      Thanks Murali Krishna, 
      we are glad, it was useful to you
      Reply