Difference between Webdriver get() and navigate()

In the previous post, we have seen how to work with different browsers in selenium. In this post,we will see the difference between Webdriver get() and navigate().

We will use Firefox browser in our demos. The same methods will work for all other browsers.

There are 2 ways, you can open the application in the browser.

  1. get() method
  2. navigate() method

get and naviagate

1. get() method: 

After opening the browser, we need to use the below method to open any particular application. This methods requires the URL of the application.

driver.get(“URL of the application”);

Don't Forget Note Means Important Remember Forgetting

URL should start with Http: instead of WWW. If URL starts with ‘WWW’, then selenium does not open it.
e.g. Valid is http://www.gmail.com
Invalid is www.gmail.com

Code for using get method to open Gmail application:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class OpenBrowsers {
	private static WebDriver driver;
	public static void main(String[] args) {
		
		driver = new FirefoxDriver();
		driver.get("http://www.gmail.com");
	}
}

2. navigate() method:

This is another method which can be used to open the application.

driver.navigate().to(“URL of application”);

Code for using navigate function to open application:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class OpenBrowsers {
	private static WebDriver driver;
	public static void main(String[] args) {
		
		driver = new FirefoxDriver();
		driver.navigate().to("http://www.gmail.com");

	}
}

What is the difference between get() method and navigate() method?

The difference is that if you use navigate, it allows you to go forth and back into history of the browser,refresh the browser etc. It is not possible if using get() method.

driver.navigate().back(); // Perform backward function of browser
driver.navigate().forward();  // Perform forward function of browser
driver.navigate().refresh(); //refresh the browser

 

Ask Question
Have any question or suggestion for us?Please feel free to post in  Q&A Forum

 

Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

2 Responses

  1. August 16, 2015

    […] Difference between Webdriver get() and navigate() […]

  2. August 16, 2015

    […] the previous post, we have seen the difference between WebDriver get() and navigate() method. In this post, we will write the first program in […]