• Selenium Video Tutorials

Selenium WebDriver - Browser Navigation



Selenium Webdriver provides multiple methods which help to achieve browser navigation which include launching, moving backward, forward on browser history, and refreshing the browser.

Some of the browser navigation methods are discussed below −

driver.navigate().to("url of browser to be launched")

This method launches the browser to open an application.

Syntax

driver.navigate().to("https://www.tutorialspoint.com/index.htm");

driver.get("url of browser to be launched")

This method launches the browser to open an application.

Syntax

driver.get("https://www.tutorialspoint.com/index.htm");

driver.navigate().back()

This method jumps to the previous page as per browser history context.

Syntax

driver.navigate().back();

driver.navigate().forward()

This method jumps forward to the next page as per browser history context.

Syntax

driver.navigate().forward();

driver.navigate().refresh()

This method reloads a page.

Syntax

driver.navigate().refresh();

Let us take an example, where we would first launch an application having the browser title Selenium Practice - Student Registration Form.

Selenium Browser Navigation 1

Then we would click on the Login link, once clicked, we would be navigated to another page having the browser title Selenium Practice - Login.

Selenium Browser Navigation 2

Next, we would navigate back to the browser history, we would again get back the previous history having the browser title Selenium Practice - Student Registration Form.

Finally, we would navigate forward to the browser history, we would again move forward in browser history having the browser title Selenium Practice - Login. We would also refresh the browser, after which we would fetch the browser title Selenium Practice - Login.

Syntax

WebDriver driver = new ChromeDriver();
driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

// Getting browser title after launch
System.out.println("Getting browser title after launch: " + driver.getTitle());

// identify a link then click
WebElement l = driver.findElement(By.xpath("<value of xpath>"));
l.click();

// navigate back to browser history
driver.navigate().back();

// navigate forward to browser history
driver.navigate().forward();

// refresh browser
driver.navigate().refresh();

Example

Code Implementation on BrowNavigation.java class file.

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class BrowNavigation {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // launching a browser and open a URL
      driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // Getting browser title after launch
      System.out.println("Getting browser title after launch: " + driver.getTitle());

      // identify the link then click
      WebElement l = driver.findElement(By.xpath("//*[@id='collapseTwo']/div/ul/li[2]/a"));
      l.click();

      // Getting browser title after clicking link
      System.out.println("Getting browser title after clicking link: " + driver.getTitle());

      // navigate back to browser history
      driver.navigate().back();

      // Getting browser title after navigating back
      System.out.println("Getting browser title after navigating back: " + driver.getTitle());

      // navigate forward to browser history
      driver.navigate().forward();

      // Getting browser title after navigating forward
      System.out.println("Getting browser title after navigating forward: " + driver.getTitle());

      // refresh browser
      driver.navigate().refresh();

      // Getting browser title after browser refresh
      System.out.println("Getting browser title after browser refresh: " + driver.getTitle());

      // Closing browser
      driver.quit();
   }
}

Output

Getting browser title after launch: Selenium Practice - Student Registration Form
Getting browser title after clicking link: Selenium Practice - Login
Getting browser title after navigating back: Selenium Practice - Student Registration Form
Getting browser title after navigating forward: Selenium Practice - Login
Getting browser title after browser refresh: Selenium Practice - Login

Process finished with exit code 0

In the above example, we had launched a URL in the opened browser and obtained the browser title with the message in the console - Getting browser title after launch: Selenium Practice - Student Registration Form. We had then clicked on the Login link and received the browser title of the navigated page with the message in the console - Getting browser title after clicking link: Selenium Practice - Login. Next, we had moved back in browser history and obtained the browser title with the message in the console - Getting browser title after navigating back: Selenium Practice - Student Registration Form.

Again, we navigated forward in browser history and retrieved the browser title with the message in the console - Getting browser title after navigating forward: Selenium Practice - Login. Finally, we refreshed the browser and got its title with the message in the console - Getting browser title after browser refresh: Selenium Practice - Login.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Let us take an example, where we would first launch an application having the URL as −

https://www.tutorialspoint.com/selenium/practice/login.php.

Selenium Browser Navigation 3

Then we would click on the Register link, once clicked, we would be navigated to another page having the URL as −

https://www.tutorialspoint.com/selenium/

Selenium Browser Navigation 4

Next, we would navigate back to the browser history, we would again get the URL as −

https://www.tutorialspoint.com/selenium/

Finally, we would navigate forward to the browser history, we would again move forward in browser history having the URL as −

https://www.tutorialspoint.com/selenium/.

We would also refresh the browser, after which we would again get the URL as −

https://www.tutorialspoint.com/selenium/.

Example

Code Implementation on BrowNavigationTo.java class file.

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class BrowNavigationTo {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // launching a browser and navigate to a URL
      driver.navigate().to("https://www.tutorialspoint.com/selenium/practice/login.php");

      // Getting browser title after launch
      System.out.println("Getting current URL after launch: " + driver.getCurrentUrl());

      // identify a link then click
      WebElement l = driver.findElement(By.xpath("//*[@id='collapseTwo']/div/ul/li[3]/a"));
      l.click();

      // Getting browser title after clicking link
      System.out.println("Getting current URL after clicking link: " + driver.getCurrentUrl());

      // navigate back to browser history
      driver.navigate().back();

      // Getting browser title after navigating back
      System.out.println("Getting current URL after navigating back: " + driver.getCurrentUrl());

      // navigate forward to browser history
      driver.navigate().forward();

      // Getting browser title after navigating forward
      System.out.println("Getting current URL after navigating forward: " + driver.getCurrentUrl());

      // refresh browser
      driver.navigate().refresh();

      // Getting browser title after browser refresh
      System.out.println("Getting current URL after browser refresh: " + driver.getCurrentUrl());

      // Closing browser
      driver.quit();
   }
}

Output

Getting current URL after launch: 
https://www.tutorialspoint.com/selenium/practice/login.php
Getting current URL after clicking link:
https://www.tutorialspoint.com/selenium/practice/register.php
Getting current URL after navigating back:
https://www.tutorialspoint.com/selenium/practice/login.php
Getting current URL after navigating forward:
https://www.tutorialspoint.com/selenium/practice/register.php
Getting current URL after browser refresh:
https://www.tutorialspoint.com/selenium/practice/register.php

Process finished with exit code 0

In the above example, we had launched a URL in the opened browser and obtained the current URLwith the message in the console - Getting current URL after launch: https://www.tutorialspoint.com/selenium/

We had then clicked on the Register link and received the current URL after navigation with the message in the console - Getting current URL after clicking link: https://www.tutorialspoint.com/selenium/

Next, we had moved back in browser history and obtained the current URL after navigation with the message in the console - Getting current URL after navigating back: https://www.tutorialspoint.com/selenium/

Again, we navigated forward in browser history and received the current URL after navigation with the message in the console - Getting current URL after navigating forward: https://www.tutorialspoint.com/selenium/

Finally, we refreshed the browser and received the current URL with the message in the console - Getting current URL after browser refresh: https://www.tutorialspoint.com/selenium/

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Thus, in this tutorial, we had discussed how to perform browser navigation using the Selenium Webdriver.

Advertisements