Navigation Commands in Selenium | To() | back() | forward() | refresh()

Navigation Commands :

Navigation commands are used to navigate from one web page to another web page, Navigation commands also helps to navigate through browser history like back forth or to refresh the current page.

 

To() :
If you wants to navigate on specific web application page or URL in between your software test then you can use¬†driver.navigate().to(‘url”) command .
driver.navigate().to("http://google.com/");

Back():

We can go back to previous page with driver.navigate().back() command.
driver.navigate().back();

Forward():

We can go forward from current page to last opened page with driver.navigate().next() command, but for using forward() method, you must have navigated back first.
driver.navigate().forward();

Refresh():

We can refresh the current page using driver.navigate().refresh() command from the navigation class.
driver.navigate().refresh();
Complete program for navigation Commands
public static void main(String args[]){ 
	System.setProperty("webdriver.chrome.driver","Path to your chromedriver.exe");
	// Open Chrome 
	WebDriver driver = new ChromeDriver();
	String webpage = "http://www.google.com";
	// Open the URL for Google 
	driver.navigate().to("http://www.google.com");
	// got to google
	driver.navigate().to(webpage);
	driver.findElement(By.id("btn_I")).click();
	// Navigate Back to google homepage 
	driver.navigate().back();
	// Navigate Forward to I am feeling lucky page
	driver.navigate().forward();
	// Refresh the page
	driver.navigate().refresh(); 
 }
Different Ways to Refresh a Page:
1.Using sendKeys.Keys method
driver.get("https://google.com");
driver.findElement(By.id("firstname-placeholder")).sendKeys(Keys.F5);
2.Using navigate.refresh()  method
driver.get("https://google.com");  
driver.navigate().refresh();
3.Using navigate.to() method
driver.get("https://google.com");  
driver.navigate().to(driver.getCurrentUrl());
4.Using get() method
driver.get("https://google.com");  
driver.get(driver.getCurrentUrl());
5.Using sendKeys() method
driver.get("https://google.com"); 
driver.findElement(By.id("firstname-placeholder")).sendKeys("\uE035");

Leave a Reply

Your email address will not be published. Required fields are marked *