Difference between Webdriver get() and Webdriver navigate()

driver.get("URL")
The first thing you’ll want to do with WebDriver is navigate to a page. The normal way to do this is by calling get:

Syntax: get(java.lang.String url) url - The URL to load. It is always best to use a fully qualified URL

Example: driver.get("http://www.google.com"); which loads a new web page in the current browser window.

Webdriver will wait until the page has fully loaded before returning the control to test or script. If there are many ajax calls in the current page which webdriver is loading then the webdriver may not know when the page has loaded completely. If you need to make sure such pages are fully loaded then you can use waits and the proceed further.

If you just use driver.get("www.google.com"); Then you will see an error some thing like below, because it treats that as an invalid URL.

org.openqa.selenium.WebDriverException: unknown error: unhandled inspector error: {"code":-32603,"message":"Cannot navigate to invalid URL"}.

driver.navigate().to("URL")

Earlier, we covered navigating to a page using the get command (driver.get("http://www.example.com")) As we have seen, webdriver has a number of smaller, task-focused interfaces, and navigation is a useful task. Because loading a page is such a fundamental requirement, the method to do this lives on the main webdriver interface, but it’s simply a synonym to:

Example: driver.navigate().to("http://www.google.com");

navigate().to() and get() do exactly the same thing. One's just a lot easier to type than the other!

There is an other overloaded method navigate().to(java.net.URL url) which makes it easy to pass in a URL. Here java.net.URL class represents a URL which has set of methods to manipulate URL in Java.

The navigate interface also has the ability to move backwards and forwards in your browser’s history:

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

We can also use Actions class of WebDriver to perform page refresh

Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();

Check here for examples on Navigation methods in webdriver
For more information please refer Selenium Docs

Selenium Tutorials: 

Comments

What is exact difference between driver.get() and driver.navigate.to() methods?

Get : 1. Waits till complete page loads. 2. can not move forward & backward in the browser.
Navigate : 1. Will not wait until the page loads, you can feel this experience only if page takes time to load, like more number of images or ajax calls etc......
2. can move forward & backward in the browser.

navigate.to() will maintain history where as get() is not.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.