Navigation Methods in Selenium WebDriver - H Y R Tutorials

Navigation Methods in Selenium WebDriver

Share This

hyrtutorials


Selenium WebDriver is an open-source tool for automated testing of web apps across many browsers.

 

Selenium Webdriver Navigation is a very important part of Webdriver API and in this video, I have explained the navigation methods in detail.

 

Selenium WebDriver has provided 4 navigation methods, Those are as follows:

  1. To
  2. Refresh
  3. Back
  4. Forward

This 4 navigation methos are available under navigation class and this navigation class instance is available under Selenium WebDriver.


1) To

Load a new web page in the current browser window. This is done using an HTTP GET operation, and the method will block until the load is complete. This will follow redirects issued either by the server or as a meta-redirect from within the returned HTML. Should a meta-redirect "rest" for any duration of time, it is best to wait until this timeout is over, since should the underlying page change whilst your test is executing the results of future calls against this interface will be against the freshly loaded page.

Eg:

driver.navigate().to("https://facebook.com");

2) Refresh

Refresh the current page

Eg:

driver.navigate().refresh();

 

3) Back

Move back a single "item" in the browser's history.

Eg:

driver.navigate().back();

 

4) Forward

Move a single "item" forward in the browser's history. It does nothing if we are on the latest page viewed.

Eg:

driver.navigate().forward();

Webdriver Navigation Methods Example:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
 
public class NavigationalMethods {
    public static void main(String[] args) throws Exception {
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.com/");
        driver.findElement(By.name("q")).sendKeys("hyr tutorials");
        driver.findElement(By.name("btnK")).submit();
        driver.navigate().to("https://facebook.com");
        driver.findElement(By.name("firstname")).sendKeys("Yadagiri");
        driver.navigate().refresh();
        Thread.sleep(2000);
        driver.navigate().back();
        Thread.sleep(2000);
        driver.navigate().forward();
    }
}