The difference between webdriver.get () and webdriver.navigate ()

What is the difference between get() and navigate() methods? Any of this, or perhaps another method, is expecting page content to load? I really need something like selenium s 1.0 WaitForPageToLoad but for using via webdriver`.

Any suggestions?

+56
java selenium-webdriver webdriver time-wait
Apr 14 '11 at 2:30 p.m.
source share
13 answers

Navigating

The first thing you want to do with WebDriver is go to the page. The usual way to do this is to call get :

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

WebDriver will wait for the page to fully load (i.e. the onload ) before returning control to your test or script. It is worth noting that if your page uses a large amount of AJAX when loading, then WebDriver may not know when it is fully loaded. If you need to ensure full loading of such pages, you can use waits .

Navigation: History and Location

Earlier, we looked at going to a page using the get command ( driver.get("http://www.example.com") ). As you have seen, WebDriver has several smaller task-oriented interfaces, and navigation is a useful task. Since page loading is such a fundamental requirement, the method for this lives on the main WebDriver interface, but it is simply a synonym:

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

Repeat: navigate().to() and get() do the same . Typing is much easier than the other!

The navigate interface also provides the ability to move back and forth in browser history:

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

(emphasis added)

+97
Apr 14 '11 at 15:00
source share

They both seem to go to this webpage and quote @matt's answer:

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

Single page applications are an exception to this.

The difference between the two methods does not come from their behavior, but from their behavior in how the application works and how the browser handles it.

navigate().to() moves to the page, changing the URL like moving forward / backward.

Whereas get() refreshes the page until the URL changes.

So, in cases where the application domain changes, both methods behave in a similar way. That is, the page is refreshed in both cases. But in single-page applications, while navigate().to() does not refresh the page, get() do.

Also, this is the reason why browser history is lost when using get() due to an application update.

Originally answered: stack overflow.site/questions/103132 / ...

+16
Nov 23 '15 at 10:46
source share

driver.get() : It was used to go to a specific website, but it does not support browser history and cookies, so we can’t use the forward and back buttons if we click that the page will not receive the schedule

driver.navigate() : it was used to go to a specific website, but it supports browser history and cookies, so we can use the forward and back buttons to move between pages during Testcase encoding

+8
Feb 26 '17 at 7:38
source share

I'm not sure if this applies here, but in the case of the protractor, when using navigate().to(...) story is saved, but when using get() it is lost.

One of my tests was unsuccessful because I used get() 2 times in a row and then executed navigate().back() . Since the story was lost, upon returning it went to the page with the error message:

 Error: Error while waiting for Protractor to sync with the page: {} 
+3
Feb 20 '14 at 15:43
source share

For what it's worth, from my testing of IE9, it looks like the difference in the urls containing the hashbang (single page application, in my case):

 http://www.example.com#page 

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

While the navigate().to("http://www.example.com#anotherpage") processed by the browser as an input address, address / location / URL, and JavaScript variables are not saved from the previous URL.

+2
May 12 '15 at 16:49
source share

navigate (). to () and get () will work the same when first used. If you use it more than once, then use navigate (). To () you can go to the previous page at any time, while you can do the same with get ().

Output: navigate (). To () stores the entire history of the current window, and get () simply reloads the page and stores any history.

+1
Feb 05 '18 at 20:30
source share

Otherwise, you may need to use 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. 

Navigation allows you to work with browser history, as I understand it.

0
Apr 14 2018-11-11T00:
source share

Both perform the same function, but driver.get (); seems more popular. driver.navigate().to(); best used when you are already in the middle of the script and you want to redirect from the 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 the browser instance, although both of them will work anyway.

0
Nov 07 '16 at
source share

According to javadoc for get () it is a synonym for Navigate.to ()

View javadoc screenshot below:

javadoc screenshot

Javadoc for get () says it all -

Load the new web page into the current browser window. This is done using an HTTP GET, and the method will block until the load is full. This will follow the redirects issued by either the server or the meta redirect from the returned HTML. If the meta redirect "rest" for any period of time, it is better to wait until this timeout is over, because if the main page changes while your test, the results of future calls against this interface will be against the just loaded page. Synonym for org.openqa.selenium.WebDriver.Navigation.to (String) .

0
Jan 31 '17 at 10:27
source share

driver.get() used to navigate to a specific URL (website) and wait for the page to load.

driver.navigate() used to navigate to a specific URL and does not wait for the page to load. It supports browser history or cookies to go back or forward.

0
Jun 16 '17 at 11:41
source share

driver.get(url) and navigate.to(url) both used to go to a specific web page. The key difference is that driver.get(url) : it does not support browser history and cookies and expects a full page load. driver.navigate.to(url) : it is also used to go to a specific web page. It supports browser history and cookies, does not load the page completely and does not have navigation between pages back, forward and refresh.

0
Apr 17 '18 at 7:49
source share

CASE 1

In the code below, I go to 3 different URLs, and when the execution goes to move the command, it goes back to the facebook 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 the code below, I used navigate () instead of get (), but both fragments (Case-1 and Case-2) work exactly the same, only the case-2 runtime is shorter than 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 that they both perform the same task, but with navigate () you can move back () or forward () in the session history.
  • navigate () is faster than get () because navigate () does not expect a full or full page to load.
0
Jun 19 '18 at 11:28
source share

To better understand this, you need to see the Selenium WebDriver architecture.

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

and find "Go to new URL." text. You will see both the GET and POST methods.

Hence the conclusion below:

The driver.get () method internally sends a Get request to the Selenium Server Standalone. Whereas the driver.navigate () method sends a Post request to the standalone Selenium server.

Hope help

0
May 25 '19 at 1:36
source share



All Articles