navigate back and forward in the browser history

Browser Navigate Back and Forward in Selenium

Last updated on

During automation, we are at times required to move back to the previous page or move forward to the next page in browser history. In this post, we will learn to perform these operations using driver.navigate command.

Navigate back in Selenium

driver.navigate().back();


Navigate forward in Browser History

driver.navigate().forward();


Sample code

public static void main(String[] args) throws InterruptedException{
	//Setting system property required for launching chrome browser
	System.setProperty("webdriver.chrome.driver", "driverExecutables\\chromedriver.exe");
	//Launching chrome browser
	WebDriver driver = new ChromeDriver();
	//Navigating to the desired website
	driver.get("https://artoftesting.com/sampleSiteForSelenium");
	//Used for demo purpose only, not required
	Thread.sleep(4000);
	//Clicking a link
	WebElement artOfTestingLogo = driver.findElement(By.cssSelector("div.navbar-header"));
	artOfTestingLogo.click();
	//Navigating back in browser 
	driver.navigate().back();
	//Used for demo purpose only, not required
	Thread.sleep(4000);
	//Navigating forward in browser 
	driver.navigate().forward();
}


That’s all we have in this post, comment below if you have any queries. Don’t forget to check our complete selenium tutorial here.

Selenium Webdriver with Java Tutorial

Leave a Comment