How to refresh browser in Selenium WebDriver using different methods

In this article, we will talk about how can we refresh browser using various methods.

Some time it is required to refresh the webpage to accomplish the testing objective. Let us see how this can be achieved.

Method1: Using navigate().refresh() method

Method2: Using JavaScriptExecutor

Method3: Using Actions Class

Mehtod4: Get the current URL & navigate using get() method

Method5: Get the current URL & navigate using navigate().to() method

Look at the below example –
package basicsOfSelenium;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class RefreshBrowserInDiffWays {

 public static void main(String[] args) {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to("https://www.google.co.in");
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  
  // Method1:  Using navigate().refresh() method
  driver.navigate().refresh();
  
  //Method2: Using JavaScriptExecutor
  JavascriptExecutor js = (JavascriptExecutor)driver;
  js.executeScript("history.go(0)");
  
  //Method3: Using Actions Class
  Actions actions = new Actions(driver);
  actions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
  
  //Mehtod4: Get the current URL & navigate using get() method
  driver.get(driver.getCurrentUrl());
  
  //Method5: Get the current URL & navigate using navigate().to() method
  driver.navigate().to(driver.getCurrentUrl());
  
 }

}

Hope this helps!!!!

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories