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?
Not sure it applies here also but in the case of protractor when using
navigate().to(...)
the history is being kept but when usingget()
it is lost.One of my test was failing because I was using
get()
2 times in a row and then doing anavigate().back()
. Because the history was lost, when going back it went to the about page and an error was thrown:(Emphasis added)
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):
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.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.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.
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
They both seems to navigate to the given webpage and quoting @matt answer:
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://stackoverflow.com/a/33868976/3619412