0

Google's Answer: 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.

Selenium Documentation: The document.readyState property of a document describes the loading state of the current document. By default, WebDriver will hold off on responding to a driver.get() (or) driver.navigate().to() call until the document ready state is complete

My query is in Google it was said, navigate method doesnot wait till the page loads which was not in line with the point added from Selenium Documentation. Please help me to understand.

mk7644
  • 33
  • 2
  • 9

3 Answers3

1

The first thing we do when run the script is to open the browser and load the web page. We use commonly driver.get(“url”); to load the webpage. Every time we use this command, the page will be refreshed.

We can also use driver.navigate().to(“url’); to load the webpage. Both the commands work in the same way in terms of behavior. But the navigate().to() also have the other functions such as navigate().forward(), navigate().back() and navigate().refresh().

So the difference is driver.get() never stores history whereas driver.navigate().to() stores browser history so as to be used for other commands forward and back etc.

In single page applications while navigate().to() navigates to the page by changing URL like doing forward/backward, get() refreshes page.

More info here - Difference between webdriver.get() and webdriver.navigate()

Swaroop Humane
  • 1,770
  • 1
  • 7
  • 17
0

In simple words 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);
    

Hence you can use:

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

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();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • My query is in Google it was said, navigate method doesnot wait till the page loads which was not in line with the point added from Selenium Documentation. Please help me to understand. – mk7644 Jul 27 '20 at 16:04
  • @mpmuthu Don't leave out anything to the imagination about what `get()` or `navigate()` will do differently. Instead, constantly keep on synchronizing the [WebDriver](https://stackoverflow.com/questions/48079120/what-is-the-difference-between-chromedriver-and-webdriver-in-selenium/48080871#48080871) instance with the _Browsing Context_. See: [Do we have any generic function to check if page has completely loaded in Selenium](https://stackoverflow.com/questions/50327132/do-we-have-any-generic-function-to-check-if-page-has-completely-loaded-in-seleni/50329056#50329056) – undetected Selenium Jul 27 '20 at 19:55
0
//Convenient
driver.get("https://selenium.dev");

//Longer way
driver.navigate().to("https://selenium.dev");

28/08/2022

There is no difference between the two, just that one is the long form and the other is the short form of Java.

https://www.selenium.dev/documentation/webdriver/browser/navigation/

boludoz
  • 36
  • 4