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

前端 未结 14 1492
悲哀的现实
悲哀的现实 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:48

    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.

    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 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

    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, both are performing the same task but with the use of navigate() you can move back() or forward() in your session's history.
    • navigate() is faster than get() because navigate() does not wait for the page to load fully or completely.

提交回复
热议问题