Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

URL Loading in Selenium Webdriver: All about get() and navigate() | Make Selenium Easy

Posted on 10/07/2018 By admin

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.

  • 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

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Uncategorized

Post navigation

Previous Post: API Testing Tutorial Part 12 – Tools For Manual & Automation API Testing | Make Selenium Easy
Next Post: API Testing Tutorial Part 3 – Understand Terms URN , URL ,URI & API | Make Selenium Easy

Related Posts

Write and validate XPath Expressions and CSS Selectors in Chrome and Firefox browser Uncategorized
Postman Tutorial Part 42 – Create Parameterized Request by Reading Data From CSV in Postman Uncategorized
image – Make Selenium Easy Uncategorized
Git Tutorial 33 – How To Restore Deleted And Committed But Not Pushed File By GIT Reset? Uncategorized
InterfaceTestng – Make Selenium Easy Uncategorized
#3. Generic Interface In Java Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark