Open In App

Difference Between get() and navigate() in Selenium

Last Updated : 10 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

An Automation tool that lets users automate the web page and test it on various browsers is known as Selenium. Selenium helps the users to load the web page through two different functions, i.e., get and navigate. Both of these functions have their advantages as well as disadvantages. In this article, we will compare the get and navigate functions in detail.

Difference Between get() and navigate() in Selenium

The following factors are considered to differentiate between the get and navigate functions in Selenium.

Factors

get()

navigate()

Interface Part

The get function is a part of the Webdriver interface.

The navigate function is a part of the Navigation interface.

Wait for webpage load

The get function waits until the webpage is loaded properly and available to return control.

The navigate function does not wait for the webpage to be loaded properly.

History tracking feature

The get function does not have the history tracking feature, thus enabling the user not to go back.

The navigate function has a history tracking feature. Thus, the user can go back to the previous web page too.

Refresh feature

The get function does not support the refresh feature.

The navigate function supports the refresh feature.

Forward feature

The get function does not support the forward function once the user has moved to the earlier webpage.

The navigate function supports the forward function and lets the user move to the next page once the user has moved to the earlier webpage.

Examples of get() and navigate() in Selenium

Now, we will explain the get() and navigate() functions with examples of both.

1. get() in Selenium

In this example, we have opened the Geeks For Geeks website (link) using the get function.

Java




//Importing the Selenium libraries
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class selenium3 {
    public static void main(String[] args) {
          
           //specify the location of the driver
           System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
      
           //Initialising the driver
           WebDriver driver = new ChromeDriver();
      
           //Launch the website
           driver.get("https://www.geeksforgeeks.org/");
       }
}


Output:

output-of-get-method-in-selenium

2. navigate() in Selenium

In this example, we have opened the Geeks For Geeks website (link) using navigate().to() function. Then, we have refreshed the browser using navigate().refresh(), click on the element having the text ‘Trending Now’. Moreover, we will now go back to the previous page using navigate().back() function and to next page using navigate().forward() function.

Java




// Using implicit wait
package selenium1;
 
//Importing selenium libraries
import java.util.concurrent.TimeUnit;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class selenium6 {
    public static void main(String[] args) throws InterruptedException {
         
        // State the chromedriver URL 
        System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
                 
        // Define and initiate the chrome driver
        WebDriver driver = new ChromeDriver();
                   
        // Open the Geeks For Geeks website
        driver.navigate().to("https://www.geeksforgeeks.org/");
         
        // Add implicit wait of 5 seconds
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
           
        // refresh the current browser
        driver.navigate().refresh();
           
        // Clicking of element having text Trending Now
        driver.findElement(By.linkText("Trending Now")).click();
         
        // Add implicit wait of 5 seconds
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
           
        // Go to previous web page
        driver.navigate().back();
           
        // Add implicit wait of 5 seconds
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
           
        // Go to next web page
        driver.navigate().forward();
        }
}


Output:

output-of-navigate-in-selenium

Conclusion

In conclusion, get and navigate functions perform almost the same action. Most commonly, the get() function is used to open a web page as it waits for the web page to load properly. However, you can choose which function to use based on various factors stated in the article.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads