Site icon TestingDocs.com

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

Introduction

In this post, we will look at the difference between driver.get() and driver.navigate() methods. Also, we will look at some code snippets to understand well.

Both methods are used to open a webpage in the browser. Each of them loads a new web page in the current browser window. Also, this is done using an HTTP GET request operation, and the methods will be blocked until the load is complete. Furthermore, both will follow redirects issued either by the server or as a meta-redirect (301) from within the returned HTML.

driver.get() method

If you look at the WebDriver API, get is defined as below  :

void get(String url);

parameter url is the URL to load. It is best to use a fully qualified URL. The URL should start with http:// or https://

e.g. http://www.testingdocs.com  or http://www.testingdocs.com are the valid ones .

Sample code

public class GetExample 
{
  private void bingSearch(final String searchTerm) {

        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.bing.com");
        WebElement bingField = driver.findElement(By.id("sb_form_q"));
        bingField.sendKeys(searchTerm);
        bingField.submit();

        (new WebDriverWait(driver, 15)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver innerdriver) {
                return innerdriver.getTitle().startsWith(searchTerm);
            }
        });

        System.out.println("PageTitle" + driver.getTitle());
        driver.quit();
    }

    @Test
    public void bingHelloTest() {
        bingSearch("Hello!");
    }

    
}

 

driver.navigate() method

Using this method we can load a new webpage just like in .get(). Also, we can traverse the browser history with forward() and back() methods. Furthermore , we can refresh the current webpage with .refresh()

String facebookURL = “https://www.facebook.com”;
driver.navigate().to(facebookURL);

overloaded version example :

String facebookURL = “https://www.facebook.com”;
driver.navigate().to(new URL(facebookURL));

Sample code example

public class NavigationExample 
{
  private void bingSearch(final String searchTerm) throws MalformedURLException {

        WebDriver driver = new FirefoxDriver();
  	 driver.navigate().to("https://www.bing.com");
        
        WebElement bingField = driver.findElement(By.id("sb_form_q"));
        bingField.sendKeys(searchTerm);
        bingField.submit();

        (new WebDriverWait(driver, 15)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver innerdriver) {
                return innerdriver.getTitle().toLowerCase()
.startsWith(searchTerm.toLowerCase());
            }
        });

        System.out.println("PageTitle" + driver.getTitle());

        // to method overloaded - another use 
        String facebookURL = "https://www.facebook.com";
        driver.navigate().to(new URL(facebookURL));
        System.out.println("PageTitle" + driver.getTitle());
        
        driver.quit();
    }

    @Test
    public void bingHelloTest() throws MalformedURLException {
        bingSearch("Hello!");
    }

    
}

 

Difference between driver.get() method and driver.navigate() method?

The difference is that if you use to navigate, we can browse the history of the browser . forward() and back() allows us to access browser history one item at a time. We can also refresh the current webpage. This facility is not possible using get() method. We can load one webpage at a time using get method.

 

Selenium WebDriver Tutorials on this website can be found at:

https://www.testingdocs.com/selenium-webdriver-tutorial

For more details on the Selenium, visit the official website at:

https://www.selenium.dev/

Exit mobile version