Archpoet
Archpoet

Reputation: 55

Difference between webdriver.get() and webdriver.navigate.to() in the case of urls containing hash and fragment identifier

I've noticed the following behavior while working with Selenium WebDriver in Firefox:

Given you open a url containing a hash: http://example.com/#app

When you try to access it again using webdriver.get() nothing happens in Firefox (the same behavior can be noticed when you select the current url manually and press Enter). If you do this in Chrome, the page would be re-opened.

But when you try to access the url again in Firefox using webdriver.navigate.to() the page is re-opened.

Can someone please explain the difference between webdriver.get() and webdriver.navigate.to() in this case?

I've seen here https://stackoverflow.com/a/5665218/3307322 that there's no difference between the two methods, but obviously in this case they behave differently.

Thank you.

Upvotes: 2

Views: 5078

Answers (2)

Manu
Manu

Reputation: 2291

They both navigate to the given webpage. From the link given in the answer, it can be concluded that:

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

But, here is a scenario that you gave which explains the difference, ie. Single-Page Applications.

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.

Upvotes: 2

debugger89
debugger89

Reputation: 2766

driver.navigate().to() and driver.get() performs exactly the same thing. There is no functional difference between the two.

get() performs navigating to a webpage and waiting for the page to load(until onload() is finished). That's all.

But the navigate interface exposes the ability to move backwards and forwards in your browser history.

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

However by looking at the url you provided it seems to be a one page web application. So there might be some restriction directly accessing the #app area.

Upvotes: 1

Related Questions