Difference between webdriver.get() and webdriver.navigate()
Asked Answered
D

15

85

What is the difference between get() and navigate() methods? Does any of this or maybe another method waits for page content to load? What do I really need is something like Selenium 1.0's WaitForPageToLoad but for using via webdriver.

Any suggestions?

Diocese answered 14/4, 2011 at 14:30 Comment(0)
A
133

Navigating

The first thing you’ll want to do with WebDriver is navigate to a page. The normal way to do this is by calling get:

driver.get("http://www.google.com");

WebDriver will wait until the page has fully loaded (that is, the onload event has fired) before returning control to your test or script. It’s worth noting that if your page uses a lot of AJAX on load then WebDriver may not know when it has completely loaded. If you need to ensure such pages are fully loaded then you can use waits.

Navigation: History and Location

Earlier, we covered navigating to a page using the get command (driver.get("http://www.example.com")) As you’ve seen, WebDriver has a number of smaller, task-focused interfaces, and navigation is a useful task. Because loading a page is such a fundamental requirement, the method to do this lives on the main WebDriver interface, but it’s simply a synonym to:

driver.navigate().to("http://www.example.com");

To reiterate: navigate().to() and get() do exactly the same thing. One's just a lot easier to type than the other!

The navigate interface also exposes the ability to move backwards and forwards in your browser’s history:

driver.navigate().forward();
driver.navigate().back();

(Emphasis added)

Alleluia answered 14/4, 2011 at 15:0 Comment(5)
Two points: credit the source unless you are the author and point to the relevant source which is the documentationFenner
@Fenner this answer has existed since April 2011. The Internet Archive doesn't show anything for that (non-official) Selenium tutorials page before 2014. Are you so sure that seleniumeasy.com is the original source? :)Alleluia
@Fenner furthermore, please note that the links in the quote in my answer point to the documentation from where I originally got those quotes. Unfortunately the first link has since rotted, but the second one works just fine.Alleluia
Yes, sorry, it is not entirely cut and paste, the headings are edited. I am not sure about its source, but judging from its content it comes from some tutorial, the one linked or another.Fenner
I suspect that they lifted their content from Stack Overflow. Note how the title of the article is pretty much identical to this question's.Alleluia
G
26

They both seems to navigate to the given webpage and quoting @matt answer:

navigate().to() and get() do exactly the same thing.

Single-Page Applications are an exception to this.

The difference between these two methods comes not from their behavior, but from the behavior in the way the application works and how browser deal with it.

navigate().to() navigates to the page by changing the URL like doing forward/backward navigation.

Whereas, get() refreshes the page to changing the URL.

So, in cases where application domain changes, both the method behaves similarly. That is, page is refreshed in both the cases. But, in single-page applications, while navigate().to() do not refreshes the page, get() do.

Moreover, this is the reason browser history is getting lost when get() is used due to application being refreshed.

Originally answered: https://mcmap.net/q/94884/-difference-between-webdriver-get-and-webdriver-navigate-to-in-the-case-of-urls-containing-hash-and-fragment-identifier

Grosswardein answered 23/11, 2015 at 10:46 Comment(0)
E
20

driver.get() : It's used to go to the particular website , But it doesn't maintain the browser History and cookies so , we can't use forward and backward button , if we click on that , page will not get schedule

driver.navigate() : it's used to go to the particular website , but it maintains the browser history and cookies, so we can use forward and backward button to navigate between the pages during the coding of Testcase

Etheleneethelin answered 26/2, 2017 at 7:38 Comment(0)
U
4

Not sure it applies here also but in the case of protractor when using navigate().to(...) the history is being kept but when using get() it is lost.

One of my test was failing because I was using get() 2 times in a row and then doing a navigate().back(). Because the history was lost, when going back it went to the about page and an error was thrown:

Error: Error while waiting for Protractor to sync with the page: {}
Urn answered 20/2, 2014 at 15:43 Comment(0)
E
4

As per the javadoc for get(), it is the synonym for Navigate.to()

View javadoc screenshot below:

javadoc screenshot

Javadoc for get() says it all -

Load a new web page in the current browser window. This is done using an HTTP GET operation, and the method will block until the load is complete. This will follow redirects issued either by the server or as a meta-redirect from within the returned HTML. Should a meta-redirect "rest" for any duration of time, it is best to wait until this timeout is over, since should the underlying page change whilst your test is executing the results of future calls against this interface will be against the freshly loaded page. Synonym for org.openqa.selenium.WebDriver.Navigation.to(String).

Esthonia answered 31/1, 2017 at 10:27 Comment(0)
S
4

driver.get() is used to navigate particular URL(website) and wait till page load.

driver.navigate() is used to navigate to particular URL and does not wait to page load. It maintains browser history or cookies to navigate back or forward.

Stlouis answered 16/6, 2017 at 11:41 Comment(0)
S
3

navigate().to() and get() will work same when you use for the first time. When you use it more than once then using navigate().to() you can come to the previous page at any time whereas you can do the same using get().

Conclusion: navigate().to() holds the entire history of the current window and get() just reload the page and hold any history.

Schwab answered 5/2, 2018 at 20:30 Comment(0)
H
2

For what it's worth, from my IE9 testing, it looks like there's a difference for URLs that contain a hashbang (a single page app, in my case):

http://www.example.com#page

The driver.get("http://www.example.com#anotherpage") method is handled by the browser as a fragment identifier and JavaScript variables are retained from the previous URL.

While, the navigate().to("http://www.example.com#anotherpage") method is handled by the browser as a address/location/URL bar input and JavaScript variables are not retained from the previous URL.

Hysterogenic answered 12/5, 2015 at 16:49 Comment(0)
S
1

There are some differences between webdriver.get() and webdriver.navigate() method.


get()

As per the API Docs get() method in the WebDriver interface extends the SearchContext and is defined as:

/**
 * Load a new web page in the current browser window. This is done using an HTTP POST operation,
 * and the method will block until the load is complete.
 * This will follow redirects issued either by the server or as a meta-redirect from within the
 * returned HTML.
 * Synonym for {@link org.openqa.selenium.WebDriver.Navigation#to(String)}.
 */
void get(String url);
    
  • Usage:

    driver.get("https://www.google.com/");
    

navigate()

On the other hand, navigate() is the abstraction which allows the WebDriver instance i.e. the driver to access the browser's history as well as to navigate to a given URL. The methods along with the usage are as follows:

  • to(java.lang.String url): Load a new web page in the current browser window.

    driver.navigate().to("https://www.google.com/");
    
  • to(java.net.URL url): Overloaded version of to(String) that makes it easy to pass in a URL.

  • refresh(): Refresh the current page.

    driver.navigate().refresh();
    
  • back(): Move back a single "item" in the browser's history.

    driver.navigate().back();
    
  • forward(): Move a single "item" forward in the browser's history.

    driver.navigate().forward();
    
Stoic answered 26/7, 2020 at 17:25 Comment(0)
W
1
  1. driver.get("url") and driver.navigate( ).to("url") both are same/synonymous.

  2. to("url") internally calling get("url") method. Please find the below image for reference.

  3. Either of them does not store history - this is the wrong information that is available on most of the blogs/websites.

  4. Below, statements 1, 2, and 3, 4 will do the same things i.e land in the given URL.

    statemnt 1: driver.get("http://www.google.com");   
    statemnt 2: driver.navigate( ).to("http://www.amazon.in");
    
    statemnt 3: driver.get("http://www.google.com");  
    statemnt 4: driver.get("http://www.amazon.in");
    
  5. Only navigate() can do different things i.e. moving back, forward, etc. But not the to("url") method.

hoover on get() method

Warm answered 20/7, 2021 at 8:23 Comment(0)
W
0

Otherwise you prob want the get method:

Load a new web page in the current browser window. This is done using an
HTTP GET operation, and the method will block until the load is complete.

Navigate allows you to work with browser history as far as i understand it.

Whitefly answered 14/4, 2011 at 15:1 Comment(2)
agree to Matt that it's possible to achieve the same result with both methodsWhitefly
Thanks a lot. Ill try windowLicker later. But my current task requires not using third-patry libs :( But Ill do try it for myself. Thanx.Diocese
D
-1

Both perform the same function but driver.get(); seems more popular. driver.navigate().to(); is best used when you are already in the middle of a script and you want to redirect from current URL to a new one. For the sake of differentiating your codes, you can use driver.get();to launch the first URL after opening a browser instance, albeit both will work either way.

Destiny answered 7/11, 2016 at 14:51 Comment(0)
J
-1

driver.get(url) and navigate.to(url) both are used to go to particular web page. The key difference is that driver.get(url): It does not maintain the browser history and cookies and wait till page fully loaded. driver.navigate.to(url):It is also used to go to particular web page.it maintain browser history and cookies and does not wait till page fully loaded and have navigation between the pages back, forward and refresh.

Jollanta answered 17/4, 2018 at 7:49 Comment(0)
I
-1

CASE-1

In the below code I navigated to 3 different URLs and when the execution comes to navigate command, it navigated back to facebook home page.

public class FirefoxInvoke {
                @Test
                public static void browserInvoke()
                {
                    System.setProperty("webdriver.gecko.driver", "gecko-driver-path");
                WebDriver driver=new FirefoxDriver();
                System.out.println("Before"+driver.getTitle());
                driver.get("http://www.google.com");
                driver.get("http://www.facebook.com");
                driver.get("http://www.india.com");
                driver.navigate().back();
                driver.quit();
                }

                public static void main(String[] args) {
                    // TODO Auto-generated method stub
            browserInvoke();
                }

            }

CASE-2:

In below code, I have used navigate() instead of get(), but both the snippets(Case-1 and Case-2) are working exactly the same, just the case-2 execution time is less than of case-1

public class FirefoxInvoke {
                @Test
                public static void browserInvoke()
                {
                    System.setProperty("webdriver.gecko.driver", "gecko-driver-path");
                WebDriver driver=new FirefoxDriver();
                System.out.println("Before"+driver.getTitle());
                driver.navigate().to("http://www.google.com");
                driver.navigate().to("http://www.facebook.com");
                driver.navigate().to("http://www.india.com");
                driver.navigate().back();
                driver.quit();
                }

                public static void main(String[] args) {
                    // TODO Auto-generated method stub
            browserInvoke();
                }

            }
  • So the main difference between get() and navigate() is, both are performing the same task but with the use of navigate() you can move back() or forward() in your session's history.
  • navigate() is faster than get() because navigate() does not wait for the page to load fully or completely.
Isiah answered 19/6, 2018 at 11:28 Comment(0)
T
-1

To get a better understanding on it, one must see the architecture of Selenium WebDriver.

Just visit https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol

and search for "Navigate to a new URL." text. You will see both methods GET and POST.

Hence the conclusion given below:

driver.get() method internally sends Get request to Selenium Server Standalone. Whereas driver.navigate() method sends Post request to Selenium Server Standalone.

Thallophyte answered 25/5, 2019 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.