Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

How To Load URL In Selenium WebDriver – get() & navigate()

Posted on 03/21/2025 By admin

Launching a web browser and loading a URL in opened browser are basic steps to get started with Selenium WebDriver. We have already learnt to launch different browsers using Selenium WebDriver. In this post, we will see how can we load a URL in browser.

In this post we will learn below topics:

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

get() method is used to load a web page or URL in a browser using an HTTP POST operation i.e.

POST /session/:sessionId/url

where :-

:sessionId – ID of the session to route the command to. url – {string} The URL to navigate to passed as body.

Implementation of get() method is in RemoteWebDriver class which creates a POST request internally (execute() method) and you have no need to worry abut creating an API request explicitly.

Method signature is as below :-

void get(java.lang.String url)

Points to know about get():

  1. get() method is declared in WebDriver interface.
  2. get() method is implemented in RemoteWebDriver class which construct an HTTP POST request.
  3. get() method returns void.
  4. get() method takes an argument of type String which is a URL.
  5. It loads a new web page in the current browser window.
  6. It blocks the execution of next statement till web page is loaded completely.
  7. If you try to open a URL without HTTP or HTTPS i.e. driver.get(“www.facebook.com”);) then get() method will throw an exception as “org.openqa.selenium.InvalidArgumentException: invalid argument“. This is common mistake done by beginners.
  8. You can load a URL without mentioning WWW i.e. http://facebook.com. It will not give any exception. This was asked to me in interview.
  9. There may be a Page Load Timeout which depends upon browser. For chrome, page load time out is shown as 300000 seconds as shown below :-

10. We can set page load time using pageLoadTimeout( long arg0, TimeUnit arg2 ) method.

12. You can go back and forward using interface Navigation method as browser history is maintained. You can use get() multiple times in a program.

package BasicSeleniumConcepts;

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

import io.github.bonigarcia.wdm.WebDriverManager;

public class LoadURLUsingGet {

        public static void main(String[] args) {

                WebDriverManager.chromedriver().setup();
                WebDriver driver = new ChromeDriver();
                System.out.println("Loading URL one by one ........");
                driver.get("http://facebook.com");
                System.out.println("Current title is : " + driver.getTitle());
                driver.get("http://google.com");
                System.out.println("Current title is : " + driver.getTitle());
                driver.get("http://flipkart.com");
                System.out.println("Current title is : " + driver.getTitle());
                System.out.println("Going back....");
                driver.navigate().back();
                System.out.println("Current title is : " + driver.getTitle());
                driver.navigate().back();
                System.out.println("Current title is : " + driver.getTitle());
                System.out.println("Going forward......");
                driver.navigate().forward();
                System.out.println("Current title is : " + driver.getTitle());
                driver.navigate().forward();
                System.out.println("Current title is : " + driver.getTitle());

        }
}

Starting ChromeDriver 81.0.4044.138 (8c6c7ba89cc9453625af54f11fd83179e23450fa-refs/branch-heads/4044@{#999}) on port 23181
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
May 23, 2020 1:30:53 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Loading URL one by one ……..
Current title is : Facebook – log in or sign up
Current title is : Google
Current title is : Online Shopping Site for Mobiles, Electronics, Furniture, Grocery, Lifestyle, Books & More. Best Offers!
Going back….
Current title is : Google
Current title is : Facebook – log in or sign up
Going forward……
Current title is : Google
Current title is : Online Shopping Site for Mobiles, Electronics, Furniture, Grocery, Lifestyle, Books & More. Best Offers!

WebDriver interface has a method “navigate()” which returns a type Navigation which is an inner interface in WebDriver interface. Navigation interface is an abstraction allowing the driver to access the browser’s history by explicit methods and to navigate to a given URL.

This Navigation interface consists of below methods :-

// Move back a single "item" in the browser's history
void back(); 
// Move a single "item" forward in the browser's history. Does nothing if 
we are on the latest page viewed.
void forward();
// Load a new web page in the current browser window. Similar to get() method
void to(String url); 
// Overloaded version of to(String url) that makes it easy to pass in a URL
void to(URL url);  
//Refresh the current page 
void refresh(); 

There is no difference between to() and get() methods as to() method internally calls get() method itself. Both methods will block execution of next line until page load is complete or timeout. Both methods holds browsing history. Navigation interface provides you direct methods to access browse history.

package BasicSeleniumConcepts;

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

import io.github.bonigarcia.wdm.WebDriverManager;

public class LoadURLUsingNavigate {

        public static void main(String[] args) {

                WebDriverManager.chromedriver().setup();
                WebDriver driver = new ChromeDriver();
                System.out.println("Loading URL one by one ........");
                driver.navigate().to("http://facebook.com");
                System.out.println("Current title is : " + driver.getTitle());
                driver.navigate().to("http://google.com");
                System.out.println("Current title is : " + driver.getTitle());
                driver.navigate().to("http://flipkart.com");
                System.out.println("Current title is : " + driver.getTitle());
                System.out.println("Going back....");
                driver.navigate().back();
                System.out.println("Current title is : " + driver.getTitle());
                driver.navigate().back();
                System.out.println("Current title is : " + driver.getTitle());
                System.out.println("Going forward......");
                driver.navigate().forward();
                System.out.println("Current title is : " + driver.getTitle());
                driver.navigate().forward();
                System.out.println("Current title is : " + driver.getTitle());

        }
}
Starting ChromeDriver 81.0.4044.138 (8c6c7ba89cc9453625af54f11fd83179e23450fa-refs/branch-heads/4044@{#999}) on port 19532
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
May 23, 2020 2:10:03 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Loading URL one by one ........
Current title is : Facebook – log in or sign up
Current title is : Google
Current title is : Online Shopping Site for Mobiles, Electronics, Furniture, Grocery, Lifestyle, Books & More. Best Offers!
Going back....
Current title is : Google
Current title is : Facebook – log in or sign up
Going forward......
Current title is : Google
Current title is : Online Shopping Site for Mobiles, Electronics, Furniture, Grocery, Lifestyle, Books & More. Best Offers!

Difference between get() and navigate():

  1. Return type of get() is void while return type of navigate() is a Navigation interface which allows you use other methods.
  2. Using navigate() you get direct methods of Navigation interface to access browsing history by using back() and forward() methods. You can navigate to a URL as well as you can refresh current window.
  3. There is no difference in internal implementation of get() and to() as to() method calls get() method internally.

I have explained difference in detailed here.

You can download/clone above sample project from here.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Test Scenarios Vs. Test Cases in Software Testing
Next Post: webdriver quiz

Related Posts

DupilicatePriResult1 – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
Postman Tutorial Part 54 – Testing SOAP API(WSDL) Using Postman Uncategorized
Part 2: Usages Of Javascripts In Selenium: How To Run Javascript Commands In Selenium Uncategorized
Make Selenium Easy – Page 19 Uncategorized
implicitwait – Make Selenium Easy 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