Difference between driver.get() and driver.navigate()
Difference between driver.get() and driver.navigate()
In this post, we will look at the difference between driver.get() and driver.navigate() methods. Both methods are used to open a webpage in the browser. Each of them loads a new web page in the current browser window. Also, this is done using an HTTP GET request operation, and the methods will be blocked until the load is complete. Furthermore, both will follow redirects issued either by the server or as a meta-redirect (301) from within the returned HTML.
driver.get() Method
If you look at the WebDriver API, get is defined as below :
void get(String url);
parameter url is the URL to load. It is best to use a fully qualified URL. The URL should start with http:// or https://
e.g. http://www.testingdocs.com or http://www.testingdocs.com are the valid ones .
Sample code Example
public class GetExample { private void bingSearch(final String searchTerm) { WebDriver driver = new FirefoxDriver(); driver.get("https://www.bing.com"); WebElement bingField = driver.findElement(By.id("sb_form_q")); bingField.sendKeys(searchTerm); bingField.submit(); (new WebDriverWait(driver, 15)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver innerdriver) { return innerdriver.getTitle().startsWith(searchTerm); } }); System.out.println("PageTitle" + driver.getTitle()); driver.quit(); } @Test public void bingHelloTest() { bingSearch("Hello!"); } }
driver.navigate() Method
Using this method we can load a new webpage just like in .get(). Also, we can traverse the browser history with forward() and back() methods. Furthermore , we can refresh the current webpage with .refresh()
String facebookURL = “https://www.facebook.com”;
driver.navigate().to(facebookURL);
overloaded version example :
String facebookURL = “https://www.facebook.com”;
driver.navigate().to(new URL(facebookURL));
Sample code example
public class NavigationExample { private void bingSearch(final String searchTerm) throws MalformedURLException { WebDriver driver = new FirefoxDriver(); driver.navigate().to("https://www.bing.com"); WebElement bingField = driver.findElement(By.id("sb_form_q")); bingField.sendKeys(searchTerm); bingField.submit(); (new WebDriverWait(driver, 15)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver innerdriver) { return innerdriver.getTitle().toLowerCase() .startsWith(searchTerm.toLowerCase()); } }); System.out.println("PageTitle" + driver.getTitle()); // to method overloaded - another use String facebookURL = "https://www.facebook.com"; driver.navigate().to(new URL(facebookURL)); System.out.println("PageTitle" + driver.getTitle()); driver.quit(); } @Test public void bingHelloTest() throws MalformedURLException { bingSearch("Hello!"); } }
driver.get() method vs driver.navigate()
The difference is that if you use it to navigate, you can browse the history of the browser . forward() and back() allow us to access browser history one item at a time. We can also refresh the current webpage. This facility is not possible using the get() method. We can load one webpage at a time using the get method.
driver.get() | driver.navigate().to() | |
---|---|---|
Basic Function | Opens a new web page in the current browser window. | Navigates to a new web page, similar to browser navigation. |
Browser History | Does not maintain browser history. You cannot go back or forward. | Maintains browser history. You can use navigate().back() , navigate().forward() , and navigate().refresh() . |
Use Case | Best for opening a specific URL directly. | Best when you need navigation controls like back, forward, or refresh. |
Page Load | Waits until the page is fully loaded before executing the next command. | Also waits until the page is fully loaded, but allows more flexible navigation. |
Syntax | driver.get("https://example.com"); |
driver.navigate().to("https://example.com"); |