Selenium WebDriver  Commands/Methods – Navigation methods Part 2

Welcome back to the Selenium Automation Testing Free training series. In this Selenium WebDriver Commands/Methods Part 2, we will talk about all Navigation methods or commands.

To read about the Browser methods/commands, please check the following link:

Read Selenium WebDriver Commands/Methods Part – 1

Selenium Webdriver Navigation methods

All right. Let’s talk about Navigation methods or commands:

1. Navigate To Command

Method: to(String arg0)

This method loads a new web page in the existing tab/browser, which we pass in the parameter.

Return: void

Parameters: one parameter for URL.

Example: 

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

The above code snippet will open the qaonlinetraining website in an existing browser’s tab.

Also read Awesome tips to become an automation tester

2. Forward Command

Method: forward()

This method enables the browser to click on the forward button.

Return: void

Parameters: no parameter

Example: 

driver.navigate().forward();

This respective command takes you forward by one page of the browser’s history.

3. Back Command

Method: back()

This method enables the browser to click on the back button.

Return: void

Parameters: no parameter

Example: 

driver.navigate().back();

This respective command takes you back by one page of the browser’s history.

Also read How should explain selenium project to the interviewer?

4. Refresh Command

Method: refresh()

This method refreshes/reloads the current web page.

Return: void

Parameters: no parameter

Example: 

driver.navigate().refresh();
 These all are navigation methods. Let’s see how to use these methods:

In this script, what we will do is

  1. we will open the qaonlinetraining website home page.
  2. Then navigate to facebook.com.
  3. Thereafter, we will go back to the qaonlinetraining home page by clicking on the back button programmatically (through WebDriver).
  4. To go back again to facebook.com, we will click on the forward button programmatically (through WebDriver).
  5. And in last, we will refresh the page and close the browser.

Selenium Webdriver script with Navigation methods

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class NavigationTestClass {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\chromedriver.exe");
        ChromeDriver driver=new ChromeDriver();
        //open the qaonlinetraining website home page
        driver.get("https://www.qaonlinetraining.com/");
        //Navigate to facebook.com
        driver.navigate().to("https://www.facebook.com");
        //go back to the qaonlinetraining home page
        driver.navigate().back();
        //go back again to facebook.com
        driver.navigate().forward();
        //refresh the page
        driver.navigate().refresh();
        //close the browser
        driver.close();
    }
}

Instructor-led Training

Important note: we like to tell you that free tutorials are useful to get started but if you are interested in the best online LIVE Master of Automation Testing training program from the experts, please refer to the following link:

For Instructor-led training

Master of Functional Automation Testing

For Self-Driven training

Automation with Selenium WebDriver (Java)

Conclusion

All right. We hope you understood all the navigation methods of Selenium WebDriver. In the next tutorial, we will talk about the last part of Web elements methods or commands.

Happy Learning until then!