Hello Folks,

We have learnt how to open Chrome, Firefox, IE and Edge browser. It is very first step before we start automating test cases through Selenium Webdriver.

Second step is to load URL of AUT(Application Under Test). So in this post we will learn below topics:

  1. Opening a URL through get() method.
  2. Opening a URL using to() method.
  3. Difference between get() and navigate()

Opening a URL through get() method:

Most common way of loading a URL in browser in Selenium is using get() method.

Syntax:  void get(java.lang.String url)

Points to know about get():

  1. get() method is declared in WebDriver interface.
  2. get() method is defined in RemoteWebDriver class.
  3. get() method returns void.
  4. get() method takes an argument of type String which is your AUT URL.
  5. It loads a new web page in the current browser window.
  6. It is done using an HTTP GET operation.
  7. It blocks the execution of next statement till web page is loaded completely.
  8. If you try to open a URL without http or https( e.g. driver.get(“www.facebook.com”);) , get() method will throw “org.openqa.selenium.WebDriverException“. It will also give error message as “Cannot navigate to invalid URL”.
  9. You can load a URL as http://facebook.com. It will not give any exception. Normally also it will load Facebook page. This was asked to me in interview.
  10. If you pass a wrong URL e.g. https://faceok.com , it will not give any page load time exception. Browser will display “ERR_CONNECTION_REFUSED” and program will be terminated. It was asked to me in interview.
  11. There is default page load time which is 300 seconds. It will throw  org.openqa.selenium.TimeoutException once default page load time reaches and page is still loading.
  12. If a URL is being loaded and you stop manually loading of URL by clicking on “Stop Loading Page” icon what will happen?? Nothing will happen and get() method will hold the control till default page load timeout reaches. You can’t make get() method fool. It is also an interview question.
  13. We can set page load time using pageLoadTimeout(long arg0, TimeUnit arg2) method. Syntax is as below:

driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

14. You can not go back and forward using get() method. You can use get() multiple times in a                  program.

Complete JAVA Program:


package MakeSeleniumEasy;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LoadURL {

public static void main(String[] args) {

System.out.println("Execution Starts");
// Setting chrome driver property and opening chrome browser
System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
WebDriver driver= new ChromeDriver();
System.out.println("Browser opened.");
// loading URL
driver.get("http://www.makeseleniumeasy.com");
System.out.println("makeseleniumeasy loaded");
driver.get("http://www.facebook.com");
System.out.println("Facebook loaded.");
// Setting default page load time. It will be applicable from next line not for above code
driver.manage().timeouts().pageLoadTimeout(1, TimeUnit.SECONDS);
driver.get("http://www.learn-automation.com");
}
}

 

Output:

In above program, I am setting page load time once Facebook is loaded. New page load time will be applicable from next statement. Remember execution of JAVA programs goes line by line. I am setting less page load time to get exception.

Opening a URL using to() method.

  • We can load a URL using to() method of Navigation inner interface of WebDriver interface.
  • It is similar to get() method and to() method calls get() method internally.

      public void to(String url) 
      {
	 RemoteWebDriver.this.get(url);
      }
  • Syntax: driver.navigate().to(“http://www.makeseleniumeasy.com”);
  • navigate() is abstract method which allows the driver to access the browser’s history and to navigate to a given URL. It returns a WebDriver.Navigation that allows the selection of what to do next.
  • It behaves in same way as get() method does.
  • Using navigate() method we can go back and forward using back() and forward() methods respectively if any session presents.
  • forward() method moves a single “item” forward in the browser’s history. Does nothing if we are on the latest page viewed.
  • back() method moves back a single “item” in the browser’s history.
  • Using navigate() method we can refresh the page by calling refresh() method on it.
  • We can pass URL class object as well to to() method to open a URL. to() method is overloaded with arguments type of String and URL(java.net.URL).
  • Syntax: driver.navigate().to(new URL(“http://www.google.com”));

Complete JAVA Program:


package MakeSeleniumEasy;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LoadURLUsingTo {

	public static void main(String[] args) {
		
		System.out.println("Execution Starts");
		// Setting chrome driver property and opening chrome browser
		System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
		WebDriver driver= new ChromeDriver();
		System.out.println("Browser opened.");
		// loading URL
		driver.navigate().to("http://www.makeseleniumeasy.com");
		System.out.println("makeseleniumeasy loaded");
		driver.get("http://www.facebook.com");
		System.out.println("Facebook loaded."); 
		driver.navigate().back();
		System.out.println("Back: "+driver.getCurrentUrl());
		driver.navigate().forward();
		System.out.println("Forward :"+driver.getCurrentUrl());
		driver.navigate().refresh();
		System.out.println("Refresh: "+driver.getCurrentUrl());
		
	}
}

Output:

Difference between get() and navigate():

  1. get() method does not allow driver to access the browser’s history while navigate() allows the driver to access the browser’s history and to navigate to a given URL.
  2. You can not navigate back and forward using get() while same can be achieved through navigate().
  3. There is no specific method for refresh a web page in get() while navigate() provides a method named refresh(). You can refresh a page using get() also by calling get() method again. It was interview question. How you will refresh a page using get() method.

That’s it guys. I hope you must have learnt some new things. If you have any doubt or suggestions, feel free to contact me.

Thank you.

#HappyLearning

 

5 thoughts on “URL Loading in Selenium Webdriver: All about get() and navigate()

  1. I read somewhere that navigate.to() does not wait for the page to load. Is that true? from above logic where both use same method for execution then it looks incorrect. Please confirm.

    1. Navigate and get both works in same way. You can check source code of methods of navigate. It internally calls get method only.

  2. Thank you for including the interview parts

    In the difference section though it has been shown as 3 points.. but what i can make out of it..

    navigate has 3 methods back forward and refresh which get does not have. is there any other difference ?

    i believe this thing is distributed among 3 points isn’t it ?

Leave a Reply