Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between webdriver.get() and webdriver.navigate()

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?

like image 275
Invy Avatar asked Apr 14 '11 14:04

Invy


People also ask

What is the get () method used for in Selenium?

The get command launches a new browser and opens the given URL in your Webdriver. It simply takes the string as your specified URL and opens it for testing purposes. If you are using Selenium IDE, it is similar to open command.

What is the difference between driver quit () and driver close () in Selenium?

close() closes only the current window on which Selenium is running automated tests. The WebDriver session, however, remains active. On the other hand, the driver. quit() method closes all browser windows and ends the WebDriver session.

What is difference between WebDriver Click () and Javascript Click ()?

The essential difference between the two methods is common to all browsers and can be explained pretty simply: WebDriver: When WebDriver does the click, it attempts as best as it can to simulate what happens when a real user uses the browser.


2 Answers

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)

like image 94
Matt Ball Avatar answered Sep 16 '22 15:09

Matt Ball


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://stackoverflow.com/a/33868976/3619412

like image 35
Manu Avatar answered Sep 18 '22 15:09

Manu