Requirement :-
How to navigate in Browser using Selenium Webdriver?
Solution :-
We are focusing on Get and Navigate Commands.
![]() |
Browser Navigation Commands |
To Command:-
This command is use to navigate to specific page or URL
driver.navigate().to(“http://www.uftHelp.com”);
Forward Command:-
This command is similar to browser forward button, Takes you forward by one page on the browser’s history.
driver.navigate().forward();
Back Command:-
Used to go back to previous page like browser’s back button.
driver.navigate().back();
Refresh Command:-
Refresh the current page.
driver.navigate().refresh();
Note:- Same can be achieved using "sendkeys"
driver.findElement(By.id(locator)).sendKeys(Keys.F5);
Get Command:-
Opens a new browser window and navigate to given URL.
driver.get(“http://www.uftHelp.com”);
Get v/s Navigate :-
“navigate().to()” and “get()” do exactly the same thing. Only thing is that incase of “get” selenium would wait for the page to fully load before executing the next line of code.
Also “navigate” interface further exposes the ability to move backwards and forwards in your browser’s history.
Let us test the above commands by completing the following Test Scenario:-
- Launch Home page using Get Method
- Open Contact Us page
- Navigate Back to Home Page
- Navigate Forward to Contact Us Page
- Refresh the Contact Us Page
- Navigate To Selenium Page
- Close the Browser
Code:-
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
public class Navigation {
public static void main(String[] args) {
//Creating WebDriver Object
System.out.println("Launch Home page using Get Method");
System.setProperty("webdriver.chrome.driver", "C:\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//Opens the given URL using Get Method
driver.get("http://www.uftHelp.com");
//Click on Contact Us Link
System.out.println("Open Contact Us page");
driver.findElement(By.id("contact-Us")).click();
//Using Back Command, go back to "Home" page
System.out.println("Navigate Back to Home Page");
driver.navigate().back();
// Using Forward Command, go to "Contact Us" link
System.out.println("Navigate Forward to Contact Us Page ");
driver.navigate().forward();
//Refresh the page
System.out.println("Refresh the Contact Us Page ");
driver.navigate().refresh();
//Using To command, open the Selenium Page
System.out.println("Navigate To Selenium Page");
driver.navigate().to("http://www.ufthelp.com/p/selenium.html");
System.out.println("Close the Browser");
//Closing the Browser
driver.close();
}
}
2 replies on “Understanding Selenium WebDriver Navigation Commands?”
THanks for the nice one
good one