Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

Selenium WebDriver is API that allows us to test web pages. In this short tutorial, we’ll look at the difference between the get() and navigate() methods in WebDriver.

2. About WebDriver

The Selenium WebDriver API contains high-level methods for interacting with different web browsers. Using this API, we can invoke different actions such as loading a web page, clicking links, searching the DOM for specific elements, and more.

Two of the methods in the API, get() and navigate(), allow us to load web pages. While they are similar in name, there are some differences in behavior as we’ll see next.

3. The get() Method

The easiest way to load a web page in WebDriver is with the get() method:

WebDriver driver = new ChromeDriver();
driver.get("https://www.baeldung.com/");

This code creates a new Chrome WebDriver and loads the Baeldung home page. Notably, the get() method waits until the web page is considered fully loaded and ready to return control. If a page has lots of JavaScript or other resources, the call may take a while.

4. The Navigate API

The WebDriver API includes a separate set of functions for navigating. Let’s see the first one:

WebDriver driver = new ChromeDriver();
driver.navigate().to("https://www.baeldung.com/");

Functionally, the navigate().to() method behaves exactly the same as the get() method. In fact, it’s just an alias for the get() method and simply loads the specified URL in the remote web browser. And because it’s just an alias for get(), it also won’t return until the web page is fully loaded.

However, the navigate API has additional abilities beyond what the get() method provides.

First, it keeps track of browser history and allows moving between pages one at a time:

driver.navigate().forward();
driver.navigate().back();

The navigate interface also allows us to refresh the current URL:

driver.navigate().refresh();

Most important, however, is that each time we use the navigate API, it maintains cookies. Unlike the get() method, which discards the session state with each call, the navigate() method does maintain state.

This means each page we load using the navigate API includes any prior cookies. This is necessary for testing many scenarios like logging in and single-page apps.

5. Conclusion

In this quick article, we looked at the difference between get() and navigate() methods in the Selenium WebDriver API. While get() is easier to use, navigate() has two primary advantages.

First, navigate() provides additional methods for navigating pages in history, as well as refreshing the current page. Second, it maintains state between each URL it navigates to, meaning cookies and other session data are persisted across each page load.

Knowing these differences allows us to choose the best method based on the needs of the test.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.