- Selenium Tutorial
- Selenium - Home
- Selenium - Overview
- Selenium - Components
- Selenium - Automation Testing
- Selenium - Environment Setup
- Selenium - Remote Control
- Selenium IDE Tutorial
- Selenium - IDE Introduction
- Selenium - Features
- Selenium - Limitations
- Selenium - Installation
- Selenium - Creating Tests
- Selenium - Creating Script
- Selenium - Store Variables
- Selenium - Alerts & Popups
- Selenium - Selenese Commands
- Selenium - Actions Commands
- Selenium - Accessors Commands
- Selenium - Assertions Commands
- Selenium - Assert/Verify Methods
- Selenium - Locating Strategies
- Selenium - Script Debugging
- Selenium - Verification Points
- Selenium - Pattern Matching
- Selenium - JSON Data File
- Selenium - Browser Execution
- Selenium - User Extensions
- Selenium - Code Export
- Selenium - JavaScript Functions
- Selenium - Plugins
- Selenium WebDriver Tutorial
- Selenium - Introduction
- Selenium WebDriver vs RC
- Selenium - Installation
- Selenium - First Test Script
- Selenium - Driver Sessions
- Selenium - Browser Options
- Selenium - Chrome Options
- Selenium - Edge Options
- Selenium - Firefox Options
- Selenium - Safari Options
- Selenium Web Elements
- Selenium - File Upload
- Selenium - Locator Strategies
- Selenium - Relative Locators
- Selenium - Finders
- Selenium - Find All Links
- Selenium - User Interactions
- Selenium - WebElement Commands
- Selenium - Browser Interactions
- Selenium - Browser Commands
- Selenium - Browser Navigation
- Selenium - Alerts & Popups
- Selenium - Handling Forms
- Selenium - Windows and Tabs
- Selenium - Handling Links
- Selenium - Input Boxes
- Selenium - Radio Button
- Selenium - Checkboxes
- Selenium - Dropdown Box
- Selenium - Handling IFrames
- Selenium - Handling Cookies
- Selenium - Date Time Picker
- Selenium - Dynamic Web Tables
- Selenium - Actions Class
- Selenium - Action Class
- Selenium - Keyboard Events
- Selenium - Key Up/Down
- Selenium - Copy and Paste
- Selenium - Handle Special Keys
- Selenium - Mouse Events
- Selenium - Drag and Drop
- Selenium - Scroll Operations
- Selenium - Waiting Strategies
- Selenium - Explicit/Implicit Wait
- Selenium - Support Features
- Selenium - Multi Select
- Selenium - Wait Support
- Selenium - Select Support
- Selenium - Color Support
- Selenium - Errors & Logging
- Selenium - Logging
- Selenium - Exception Handling
- Selenium - Miscellaneous
- Selenium - Handling Ajax Calls
- Selenium - JSON Data File
- Selenium - CSV Data File
- Selenium - Excel Data File
- Selenium - Cross Browser Testing
- Selenium - Multi Browser Testing
- Selenium - Multi Windows Testing
- Selenium - JavaScript Executor
- Selenium - Headless Execution
- Selenium - Capture Screenshots
- Selenium - Capture Videos
- Selenium - Page Object Model
- Selenium - Page Factory
- Selenium - Record & Playback
- Selenium - Frameworks
- Selenium - Browsing Context
- Selenium Grid Tutorial
- Selenium - Overview
- Selenium - Architecture
- Selenium - Components
- Selenium - Configuration
- Selenium - Create Test Script
- Selenium - Test Execution
- Selenium - Endpoints
- Selenium - Customizing a Node
- Selenium Reporting Tools
- Selenium - Reporting Tools
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & other Technologies
- Selenium - Java Tutorial
- Selenium - Python Tutorial
- Selenium - C# Tutorial
- Selenium - Javascript Tutorial
- Selenium - Kotlin Tutorial
- Selenium - Ruby Tutorial
- Selenium - Maven & Jenkins
- Selenium - Database Testing
- Selenium - LogExpert Logging
- Selenium - Log4j Logging
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash Testing
- Selenium - Apache Ant
- Selenium - Github
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
- Selenium Miscellaneous Concepts
- Selenium - Automation Frameworks
- Selenium - Keyword Driven Framework
- Selenium - Data Driven Framework
- Selenium - Hybrid Driven Framework
- Selenium - SSL Certificate Error
- Selenium - Alternatives
- Selenium Useful Resources
- Selenium - Quick Guide
- Selenium - Useful Resources
- Selenium - Automation Practice
- Selenium - Discussion
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.
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, 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.
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/
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.
To Continue Learning Please Login
Login with Google