Make Selenium Easy

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

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()

Opening a URL through get() method:

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(“”);) 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. 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.

Complete JAVA Program:


Opening a URL using to() method

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 :-

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.

Complete JAVA Program:


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.

Find all Selenium related post here, all API manual and automation related posts here and find frequently asked Java Programs here.

Many other topics you can navigate through menu.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

4 thoughts on “How To Load URL In Selenium WebDriver – get() & navigate()

  1. I read somewhere that 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

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.