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 // 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:
Difference Between get() and navigate() in Selenium
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.