Browser Navigation Commands Selenium WebDriver Tutorials

Navigation Commands In Selenium WebDriver: In our previous posts we have discussed many articles on different topics like Different locators in Selenium WebDriver, How to set up an Eclipse project, and How to Run your first Selenium program like this. In this post, we are going to discuss the different navigation commands.

After successfully running the first selenium program, we can learn about the various navigational commands. So, in this post, we plan to share the navigation common that we are using regularly in our automation testing script. A browser has various options for navigation, like:

  • Open a Web Page URL
  • Navigate to another web page by Clicking any element
  • Go Back
  • Go forward
  • Refresh a web page

Let us go through each of the commands individually and learn how to do so with the help of Selenium WebDriver.

Different Navigation Commands In Selenium

Different Selenium Navigate Commands
Different Selenium Navigate Commands

Navigate.To(URL):

In our previous posts, we have seen that with the help of the get() method, we can open a web page URL. But the Navigation interface also provides one method, and we can open a web page by using that method. To open a web page using the Navigation method, you must use the to() method.

package com.selenium.practice.basic;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Navigate_To_PGM 
{
   public static void main(String[] args) 
   {
      WebDriver driver;
      System.setProperty("webdriver.gecko.driver", "Path Of the Borwser driver file");
      
      driver = new FirefoxDriver();
      driver.navigate().to("https://www.google.com");
      driver.close();

   }
}

Navigate.To(String):

This method will load a new web page on the current browser window. All the processes are done with the help of the Get operation, and this method will block all other options until the load process is complete.

Note: The URL should be a full URL; for example, if you are trying to open the Google Home page, you must mention the full URL.

  • Valid: https://www.google.com
  • Invalid: www.google.com
package com.selenium.practice.basic;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Navigate_To_String_PGM 
{
   public static void main(String[] args) 
   {
      WebDriver driver;
      String url="https://www.google.com";
      System.setProperty("webdriver.gecko.driver", "Path of Browser Driver File");
      
      driver = new FirefoxDriver();
      driver.navigate().to(url);
      driver.close();
   }
}

Navigate.Back():

If you have redirected to other pages (Inner page) from a Home page and completed your required operation on the redirected page (Inner page), and again, you want to go back to the home page. Then, in that case, you can use the back() method of the Navigate interface.

This method moves back to a single page in the web history, and if you are on the home page, then this method will not perform any action.

Navigate.Forward():

Like the back() method, we can use the forward() method when we require to move one page forward in the web history. It will not perform any operation if you are on the web history’s last or latest page.

Navigate.Refresh():

If your requirement is reloading the complete page, then in the navigate interface, we have a method called refresh(). You can use this method to refresh the complete web page.

package com.selenium.practice.basic;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Navigate_Back_Forward_Pgm 
{

   public static void main(String[] args) throws InterruptedException 
   {
      WebDriver driver;
      System.setProperty("webdriver.gecko.driver", "F:\\Software_Testingo_Practice\\Selenium_Practice\\Selenium_Practice_PGM\\Browser_Driver\\geckodriver.exe");
      
      driver = new FirefoxDriver();
      driver.navigate().to("https://www.google.com");
      
      driver.manage().timeouts().implicitlyWait(3000, TimeUnit.SECONDS);
      driver.findElement(By.linkText("Gmail")).click();
      Thread.sleep(5000);
      System.out.println("Gmail Page Sucessfully open");
      
      driver.navigate().back();
      Thread.sleep(5000);
      System.out.println("Back to Google Home Page");
      
      driver.navigate().forward();
      Thread.sleep(5000);
      System.out.println("Gmail Page Sucessfully open By Forward");
      
      
      driver.navigate().refresh();
      Thread.sleep(5000);
      
      driver.close();

   }
}

Add a Link to the Difference between Get() and TO() methods.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment