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.
Basic Browser Navigation Commands
Some of the browser navigation methods are discussed below −
driver.navigate().to("application url")
This method navigates to an application url.
Syntax
driver.navigate().to("https://www.tutorialspoint.com/index.htm");
driver.get("application url")
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 web page.
Syntax
driver.navigate().refresh();
Example 1
Let us take an example, where we would first launch an application having the browser title Selenium Practice - Student Registration Form.
Then we would click on the Login link, once clicked, we would be navigated to another page having the browser title Selenium Practice - Login.
Next, we would navigate back to the browser history, and get the browser title as Selenium Practice - Student Registration Form.
Finally, we would navigate forward to the browser history, and get the browser title as Selenium Practice - Login. We would also refresh the browser, and obtain the browser title as Selenium Practice - Login.
Code Implementation
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
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.
Example 2
Let us take an example, where we would first launch an application having the URL as −
https://www.tutorialspoint.com/selenium/practice/login.php.
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/practice/register.php.
Next, we would navigate back to the browser history, and get the URL as −
https://www.tutorialspoint.com/selenium/practice/login.php.
Finally, we would navigate forward to the browser history, and get the URL as −
https://www.tutorialspoint.com/selenium/practice/register.php.
We would also refresh the browser, after which we would obtain the URL as
https://www.tutorialspoint.com/selenium/practice/register.php.
Code Implementation
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
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/practice/login.php.
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/practice/register.php.
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/practice/login.php.
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/practice/register.php.
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/practice/register.php.
Thus, in this tutorial, we had discussed how to perform browser navigation using the Selenium Webdriver.