Training Outcomes Within Your Budget!

We ensure quality, budget-alignment, and timely delivery by our expert instructors.

Share this Resource

Table of Contents

Navigation Commands in Selenium

Selenium is an open-source umbrella project aimed at software testing for web applications. It provides a playback tool for authoring tests across web browsers without a test scripting language. 

When testing web-based applications, one of the most essential aspects is navigation. During testing, Browser Navigation Commands in Selenium are used to move from one web page to another. According to Slintel, Selenium holds nearly 46% of the market share because of its ease of use compared to its software testing rivals. Navigation commands in Selenium helps to manage the browser's navigation action during test execution. Discover the many and most used Selenium navigation commands. 

Table of Contents 

1) Five most commonly used Navigation Commands in Selenium

         a) get()

         b) back()

         c) forward()

         d) to()

        e) refresh()

2) Using Navigation Commands in Automated Web Testing

3) Conclusion 

Five most commonly used Navigation Commands in Selenium

The Browser Navigation Commands in Selenium are commands we  use in our day-to-day Automation Testing. The navigation interface allows the user to move forwards and backwards in the browser's history. This section of the blog shall discuss the five most commonly used Navigation Commands:

get(): 

The get() command loads a new web page in the current browser window. This method takes a URL as an argument and loads the URL in the current browser window. The syntax for the get() command is: 
driver.get("https://selenium.dev"); 

Example of usage of the get() command: 
 

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Example {
  public static void main(String[] args) {
    // Set the path of the chromedriver executable
    System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
    // Create a new instance of ChromeDriver
    WebDriver driver = new ChromeDriver();
    // Navigate to the Google homepage
    driver.get("https://www.google.com");
    // Close the browser window and end the WebDriver session
    driver.quit();
  }
}


In this example, we import the necessary packages and classes, set the path of the chromedriver executable, create a new instance of ChromeDriver, and use the get() method to navigate to the Google homepage. 

Finally, we close the browser window and end the WebDriver session using the quit() method. 
 

Selenium Training
 

back(): 

The back() command moves back to the previous web page in the browser history. This method simulates the back button click in the browser. The syntax for the back() command is: 

driver.navigate().back(); 

Example of usage of the back() command: 
 

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Example {
  public static void main(String[] args) {
    // Set the path of the chromedriver executable
   System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
   // Create a new instance of ChromeDriver
   WebDriver driver = new ChromeDriver();
   // Navigate to the Google homepage
   driver.get("https://www.google.com");
   // Navigate to the Selenium homepage
   driver.get("https://www.selenium.dev");
   // Navigate back to the Google homepage
   driver.navigate().back();
   // Close the browser window and end the WebDriver session
   driver.quit();
  }
}


In this example, we import the necessary packages and classes, set the path of the chromedriver executable, create a new instance of ChromeDriver, and use the get() method to navigate to the Google homepage and then the Selenium homepage. 

We then use the navigate().back() method to navigate back to the Google homepage. 

Finally, we close the browser window and end the WebDriver session using the quit() method. 

forward():

The forward() command moves to the next web page in the browser history. This method simulates the forward button click in the browser. The syntax for the forward() command is: 
driver.navigate().forward(); 

Example of usage of the forward() command: 
 

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Example {
  public static void main(String[] args) {
    // Set the path of the chromedriver executable
   System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
   // Create a new instance of ChromeDriver
   WebDriver driver = new ChromeDriver();
   // Navigate to the Google homepage
   driver.get("https://www.google.com");
   // Navigate to the Selenium homepage
   driver.get("https://www.selenium.dev");
   // Navigate back to the Google homepage
   driver.navigate().back();
   // Navigate forward to the Selenium homepage again
   driver.navigate().forward();
   // Close the browser window and end the WebDriver session
   driver.quit();
  }
}


In this example, we import the necessary packages and classes, set the path of the chromedriver executable, create a new instance of ChromeDriver, and use the get() method to navigate to the Google homepage and then the Selenium homepage. 

We then use the navigate().back() method to navigate back to the Google homepage, and then use the navigate().forward() method to navigate forward to the Selenium homepage again. 

Finally, we close the browser window and end the WebDriver session using the quit() method. 

to(): 

The to() command is used to navigate a new URL using a webdriver. This method is similar to the get() method but can be used with other webdrivers such as the FirefoxDriver or the EdgeDriver. The syntax for the to() command is as follows: 

driver.navigate().to("https://selenium.dev"); 

Example of usage of the to() command: 
 

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Example {
  public static void main(String[] args) {
   // Set the path of the chromedriver executable
   System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
   // Create a new instance of ChromeDriver
   WebDriver driver = new ChromeDriver();
   // Navigate to the Google homepage
   driver.navigate().to("https://www.google.com");
   // Navigate to the Selenium homepage
   driver.navigate().to("https://www.selenium.dev");
   // Close the browser window and end the WebDriver session
   driver.quit();
  }
}


In this example, we import the necessary packages and classes, set the path of the chromedriver executable, create a new instance of ChromeDriver, and use the navigate().to() method to navigate to the Google homepage and then the Selenium homepage. 

Finally, we close the browser window and end the WebDriver session using the quit() method. 

refresh(): 

The refresh() command is used to refresh the current web page. This method simulates the refresh button click in the browser. The syntax for the refresh() command is as follows: 
driver.navigate().refresh(); 

Example of usage of the refresh() command: 
 

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Example {
  public static void main(String[] args) {
   // Set the path of the chromedriver executable
   System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
   // Create a new instance of ChromeDriver
   WebDriver driver = new ChromeDriver();
   // Navigate to the Google homepage
   driver.get("https://www.google.com");
   // Refresh the page
   driver.navigate().refresh();
   // Close the browser window and end the WebDriver session
   driver.quit();
  }
}


In this example, we import the necessary packages and classes, set the path of the chromedriver executable, create a new instance of ChromeDriver, and use the get() method to navigate to the Google homepage. 

We then use the navigate().refresh() method to refresh the page. 

Finally, we close the browser window and end the WebDriver session using the quit() method.

Using Navigation Commands in Automated Web Testing

Commands for navigation play a crucial role in Automated Web Testing, allowing testers to move across diverse pages and engage with different elements on the web page. Now you can use these commands to facilitate the emulation of your actions, including clicking on links, navigating forward and backward, and refreshing the page. The subsequent scenarios highlight the applications of navigation commands in Automated Web Testing:

Navigation to a user login page

In the context of Automated Web Testing, it's typical to initiate a test by directing the automation to the login page of the web application. This can be achieved using the get(String url) or navigate().to(String url) command.

E.g. driver.get("https://www.example.com/login");

Clicking links and navigating to different pages

In the domain of Automated Web Testing, it is common to move through various pages and select links to reach different segments of the web application. The command navigate().to(String url) can be utilised for directing to a particular page, whereas navigate().back() and navigate().forward() commands facilitate moving backward and forward between pages.

e.g. driver.get("https://www.example.com/home"); WebElement contactLink = driver.findElement(By.linkText("Contact Us"));contactLink.click(); driver.navigate().back();

Refreshing the page

In automated web testing, it's a standard practice to refresh the page in Selenium after performing a specific action to verify that any modifications have been successfully preserved. The command navigate().refresh() can be employed for this purpose.

E.g. driver.navigate().refresh();

Handling page redirects

Certain web applications utilise page redirects to guide users to various pages or sections within the application. In Automated Web Testing, it's crucial to manage page redirects to guarantee the proper execution of the test. The provided code snippet demonstrates the utilisation of the wait functionality in Selenium WebDriver for handling page redirects.

driver.get("https://www.example.com/page-redirect"); WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.urlContains("redirected-page"));

Handling pop-ups and alerts

Web applications frequently utilise pop-ups and alerts to present messages or request user confirmation for specific actions. In an Automated Web Testing scenario, the effective management of these pop-ups and alerts is crucial to guarantee the proper execution of tests. The provided code excerpt demonstrates the utilisation of the Alert interface in Selenium WebDriver for handling alerts.

WebElement alertButton = driver.findElement(By.id("alert-button"));

alertButton.click();

WebDriverWait wait = new WebDriverWait(driver, 10);

Alert alert = wait.until(ExpectedConditions.alertIsPresent());

String alertText = alert.getText();

alert.dismiss();

Navigating between frames

Website pages generally encompass many frames and Automated Web Testing. Such pages need navigation among these frames for the purpose of interacting with the many elements contained within them. 

E.g. here is a short code snippet that demonstrates the usage of the switchTo() method in Selenium WebDriver.

driver.switchTo().frame(0);

driver.switchTo().parentFrame();

driver.switchTo().defaultContent();

Conclusion

Selenium offers a wide range of Browser Navigation Commands to automate web browsing and testing by also specifying system set property for specific browsers. This blog covered the five most widely-used Browser Navigation Commands in Selenium. These Navigation Commands are also basic so that beginners to software testing and automation can practise and perform.   

Learn more about automation with Introduction To Test Automation With Selenium Web Driver. Sign up now! 

Frequently Asked Questions

What is navigation simple? faq-arrow

Navigation in Selenium is a function that users can take the help of, to control and interact with the various web pages during the process of their automated tests. Such methods allow software testers to find their ways through different web pages, interaction with elements and doing actions like clicking on links, submitting user forms or refreshing the web page.

What is XPath in Selenium? faq-arrow

 XPath is a technique in Selenium that is applied for navigational purposes through a website page’s HTML structure. It is designed to be a language or a syntax which helps make the discovery of a website’s elements easier with the use of XML path expression. Additionally, in Selenium automation, users may encounter moments when the elements they are looking for cannot be found with locators such as name, class, ID, and so on. Such situations can be resolved by using XPath to locate the needed elements on the webpage in question. Moreover, XPath in Selenium WebDriver can be utilised with XML as well as HTML documents. 

What is Maven in Selenium? faq-arrow

Maven is a tool built and released by the Apache Software Foundation for the purpose of letting users automate their test procedures. It is typically utilised to handle their Java projects, and was first developed for the simplification of the Jakarta Turbine Project’s build process. In current times, it is utilised to handle dependencies in their projects as well as the project’s lifecycle. The software tool was designed to help users easily handle their code generation, testing, validation and more. Users can also work with the tool in phases rather than small tasks and make their build process easier. 

What are the other resources and offers provided by The Knowledge Academy? faq-arrow

The Knowledge Academy takes global learning to new heights, offering over 30,000 online courses across 490+ locations in 220 countries. This expansive reach ensures accessibility and convenience for learners worldwide.  

Alongside our diverse Online Course Catalogue, encompassing 17 major categories, we go the extra mile by providing a plethora of free educational Online Resources like News updates, blogs, videos, webinars, and interview questions. Tailoring learning experiences further, professionals can maximise value with customisable Course Bundles of TKA.  

The Knowledge Academy’s Knowledge Pass, a prepaid voucher, adds another layer of flexibility, allowing course bookings over a 12-month period. Join us on a journey where education knows no bounds.

 

What are related Selenium courses and blogs provided by The Knowledge Academy? faq-arrow

The Knowledge Academy offers various Selenium courses, including Introduction to Test Automation with Selenium Web Driver, Selenium Immersion with C#, and Selenium Testing Framework Training. These courses cater to different skill levels, providing comprehensive insights into Software Testing methodologies.  

Our Software Testing blogs covers a range of topics related to Functional and Manual Testing, offering valuable resources, best practices, and industry insights. Whether you are a beginner or looking to advance your Software Testing skills, The Knowledge Academy's diverse courses and informative blogs have you covered.

 

Get A Quote

WHO WILL BE FUNDING THE COURSE?

cross

BIGGEST
Christmas SALE!

red-starWHO WILL BE FUNDING THE COURSE?

close

close

Thank you for your enquiry!

One of our training experts will be in touch shortly to go over your training requirements.

close

close

Press esc to close

close close

Back to course information

Thank you for your enquiry!

One of our training experts will be in touch shortly to go overy your training requirements.

close close

Thank you for your enquiry!

One of our training experts will be in touch shortly to go over your training requirements.