Browser Navigation Commands In Selenium

Browser Navigation Commands In Selenium

Selenium comes preloaded with many cool features and simulating the browser navigation is one of them. It allows a user to simulate forward, back, refresh etc. commands from selenium.

Selenium provides below browser navigation commands:

 

Browser Navigation Commands in Selenium

These options come under driver.navigate. and are self-explanatory. In this tutorial, we will utilize navigate.to(String URL), navigate.back() and navigate.forward() commands.

 

Case Study

Following are the steps which we will automate using browser navigation commands in selenium:

 

Browser navigation commands case study
  • Once COMPANIES page is loaded, print the first news item (a) on the console.
  • Use driver.navigate.back() command to navigate back to the homepage.
  • Use driver.navigate.forward() command to simulate browser forward command and again navigate to the COMPANIES page.
  • Once COMPANIES page is loaded, print the second news item (b) on the console.
Browser navigation commands applied selenium


Setup Project In Eclipse

Visit THIS link to learn how to create a new Java project, associate selenium jars and configure ChromeDriver. Create a package browser_navigation inside this project and create a class BrowserNavigation.

 

This website displays a notification prompt and we have to suppress it to avoid script failure.

Click HERE to learn how to disable chrome notification prompts

Below is the code to automate the steps mentioned in the Case Study:

package browser_navigation;

import java.util.HashMap;
import java.util.Map;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class BrowserNavigation {
    
    public static WebDriver driver;
    
    public static void main(String[] args) {
        // set system property and initialize the chrome driver
                System.setProperty("webdriver.chrome.driver", "/home/dhawal/Downloads/chromedriver/chromedriver");
                
                
                //suppress the notification pop up
                Map<String, Object> prefs = new HashMap<String, Object>();
                prefs.put("profile.default_content_setting_values.notifications", 2);
                ChromeOptions options = new ChromeOptions();
                options.setExperimentalOption("prefs", prefs);
                driver = new ChromeDriver(options);
                
                driver.manage().window().maximize();
                driver.navigate().to("https://www.business-standard.com/");
                
                //wait until COMPANIES link is displayed
                WebDriverWait wait = new WebDriverWait(driver, 10L);
                wait.until(ExpectedConditions.presenceOfElementLocated(By.id("nav_menu_222")));
                
                //variable to refer COMPANIES link
                WebElement companies = driver.findElement(By.id("nav_menu_222"));
                    
                                
                //click on COMPANIES link
                companies.click();
                
                //wait until the first news link is displayed
                WebDriverWait wait_first_news = new WebDriverWait(driver, 10L);
                wait_first_news.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//h2[@class='webexclu0']")));
                
                //store the reference of first news item in first_news and print it 
                WebElement first_news = driver.findElement(By.xpath("//h2[@class='webexclu0']"));
                System.out.println(first_news.getText());
                
                //navigate back to previous page
                driver.navigate().back();
                
                //wait until COMPANIES link is displayed
                WebDriverWait wait_companies = new WebDriverWait(driver, 10L);
                wait_companies.until(ExpectedConditions.presenceOfElementLocated(By.id("nav_menu_222")));
                
                //Simulate browse forward
                driver.navigate().forward();
                
                //wait until the second news link is displayed
                WebDriverWait wait_second_news = new WebDriverWait(driver, 10L);
                wait_second_news.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//h2[@class='webexclu1']")));
                
                //store the reference of second news item in second_news and print it 
                WebElement second_news = driver.findElement(By.xpath("//h2[@class='webexclu1']"));
                System.out.println(second_news.getText());
            

    }

}

Run the above code and it should execute as follows:

  • The browser is opened and navigate to the application url.
  • Click on COMPANIES link and print the first news item.
  • Simulate the browser back command, application homepage is displayed.
  • Simulate the browser forward command and it should again display news under COMPANIES.
  • Print the second news item.

Below is the output of above code:

 

Also Read: How to create xPath in Selenium | Become Master in xPath

Author: Dhawal Joshi

A post-graduate in MCA, ISTQB & ITIL certified QA with more than 8 years of experience in QA working with a CMMI Level 5 organization as System Analyst. I started my automation journey with HP UFT(formerly known as QTP) and for the past few years, I am using Selenium for automation. I also have experience in Android Application Development, Java, HTML, and VBScript. When I am not working, I like to spend time with my family, cooking and learning new developments in IT.

1 thought on “Browser Navigation Commands In Selenium

Leave a Reply

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