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

前端 未结 14 1425
悲哀的现实
悲哀的现实 2020-11-28 04:27

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 rea

相关标签:
14条回答
  • 2020-11-28 04:32

    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.

    0 讨论(0)
  • 2020-11-28 04:34

    navigate().to() and get() will work same when you use for the first time. When you use it more than once then using navigate().to() you can come to the previous page at any time whereas you can do the same using get().

    Conclusion: navigate().to() holds the entire history of the current window and get() just reload the page and hold any history.

    0 讨论(0)
  • 2020-11-28 04:37

    As per the javadoc for get(), it is the synonym for Navigate.to()

    View javadoc screenshot below:

    Javadoc for get() says it all -

    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. This will follow redirects issued either by the server or as a meta-redirect from within the returned HTML. Should a meta-redirect "rest" for any duration of time, it is best to wait until this timeout is over, since should the underlying page change whilst your test is executing the results of future calls against this interface will be against the freshly loaded page. Synonym for org.openqa.selenium.WebDriver.Navigation.to(String).

    0 讨论(0)
  • 2020-11-28 04:39

    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)

    0 讨论(0)
  • 2020-11-28 04:40

    There are some differences between webdriver.get() and webdriver.navigate() method.


    get()

    As per the API Docs 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);
        
    
    • Usage:

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

    navigate()

    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();
      
    0 讨论(0)
  • 2020-11-28 04:40

    To get a better understanding on it, one must see the architecture of Selenium WebDriver.

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

    and search for "Navigate to a new URL." text. You will see both methods GET and POST.

    Hence the conclusion given below:

    driver.get() method internally sends Get request to Selenium Server Standalone. Whereas driver.navigate() method sends Post request to Selenium Server Standalone.

    Hope it helps

    0 讨论(0)
提交回复
热议问题