WebDriver Navigation Commands – Navigate, Forward, Back, Refresh in Selenium WebDriver

HOME 

 

Navigation Commands in Selenium WebDriver are use to open a web page URL, navigate to other web page by clicking any element, Back, Forward or Refresh the web page using browser’s history. In this blog, we will discuss about below mentioned commands

1) Navigate
2) Forward
3) Back
4) Refresh

1) Navigate – This command loads a new web page in the current window as mentioned in the argument. This command accepts a string parameter and return nothing.

This command perform the same action as get() command.

Syntax:

to(String arg0):void

Command:

driver.navigate().to(URL);

2) Forward– This command act as same as clicking on Forward button of any web browser. This command does not require any parameter and void means return nothing. 

Syntax:

forward(): void 

Command:

driver.navigate().forward();

3) Back – This command act as same as clicking on Back button of any web browser. This command does not require any parameter and void means return nothing. 

Syntax:

back(): void

Command:

driver.navigate().back();

4) Refresh – This command refresh the current page. This command does not require any parameter and void means return nothing. 

Syntax:

 refresh(): void 

Command:

driver.navigate().refresh();

How to use Navigation Commands in Selenium WebDriver:

  • Set geckodriver path to launch browser.
  • Create a instance of ChromeDriver() to handle Navigation commands.
  • Wait until Page Load.
  • Navigate to URL – https://configureselenium.blogspot.com
  • Navigate to another URL.
  • Navigate to back page.
  • Navigate to forward page.
  • Refresh current page.
  • Close the browser
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; 
 
public class Navigation_Commands {
        public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver","C:\\Users\\Vibha\\Desktop\\Drivers\\chromedriver_win32\\chromedriver.exe");
        
        // Create a new instance of the Firefox driver
        WebDriver driver= new ChromeDriver();
       
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
       
        //Launch a new web page and load page using navigation Command
        driver.navigate().to("https://configureselenium.blogspot.com/");
       
        //Get the Title of web page
        String PageTitle= driver.getTitle();
        System.out.println("Title of page is :"+PageTitle);
        
        //Click on READ MORE link. New Page is opened
        driver.findElement(By.linkText("READ MORE")).click();
        String PageTitle1= driver.getTitle();
        System.out.println("Title of new page is :"+PageTitle1);
      
        // Go back to main Page
        driver.navigate().back();
        System.out.println("Go back to Main page");
             
        // Go forward to WebDriver - Browser Commands page
        driver.navigate().forward();       
        String PageTitle2= driver.getTitle();
        System.out.println("Title of page after forward option is :"+PageTitle2);
        
        //Refresh browser
        driver.navigate().refresh();
        System.out.println("Refresh browser");
       
        //Close browser
        driver.quit();
    }
 }

Output
Title of page is :Selenium
Title of new page is :How to automate Radio Button in Selenium WebDriver
Go back to Main page
Title of page after forward option is :How to automate Radio Button in Selenium WebDriver
Refresh browser

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s