All Ways Of Refreshing a Webapge In Selenium

Hello folks,

We have seen in last post which explains  how can we load a URL without using any get() and navigate() methods. In this post we will learn in how many ways we can refresh a web page. It is also frequently asked interview question.

During developing automation test scripts, we need to refresh a web page. Manually we can refresh a web page using keys such as F5, ctrl+R or using context menu reload option. We can refresh a web page in many ways in Selenium Webdriver. We will see all ways below:

  1. By simulating keys actions using Actions and Robot classes.
  2. Using selenium methods.
  3. Using javascripts methods.

By simulating keys actions:

We can simulate keyboard actions using Actions class (Provided by Selenium) and Robot class(Provided by Java AWT).

Using Robot Class:

This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.

Manually, we use CTRL+R to reload a page which is key event. We will simulate same key event using Robot class as below.


Robot r= new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_R);
r.keyRelease(KeyEvent.VK_R);
r.keyRelease(KeyEvent.VK_CONTROL);

We also use F5 to reload a page. We can simulate same key events using Robot class as below:


Robot r= new Robot();
r.keyPress(KeyEvent.VK_F5);
r.keyRelease(KeyEvent.VK_F5);
Using Actions class:

Actions class which is a user-facing API provided by Selenium is also used for emulating complex user gestures such as mouse and keyboard events. We can achieve keyboard events using Actions class as well.


//Using F5 key
Actions a= new Actions(driver);
Action a1= a.keyDown(Keys.F5).keyUp(Keys.F5).build();
a1.perform();

//using ctrl+R

Actions a= new Actions(driver);
Action a1= a.keyDown(Keys.CONTROL).sendKeys("r").keyUp(Keys.CONTROL).build();
a1.perform();

Problem with Actions class is that it may work or may not depending on browser and selenium version.So if you want to choose from Actions or Robot, go for Robot class. It will work in all browsers with all version of selenium.

Using Methods:

Methods are best way of refreshing a web page. Methods are easy and convenient to use.

get() method:

driver.get(driver.getCurrentUrl());

navigate() method:

driver.navigate().refresh();
driver.navigate().to(driver.getCurrentUrl());

sendKeys() method:
// using F5
driver.findElement(By.tagName("a")).sendKeys(Keys.F5);
// Using Unicode value for F5
driver.findElement(By.tagName("a")).sendKeys("\uE035");
// using ctrl+R
driver.findElement(By.tagName("a")).sendKeys(Keys.chord(Keys.CONTROL,"r"));

Above command may or may not work based on browser and selenium version.

Using JavaScripts:

using go() method:
WebDriver driver= new ChromeDriver();
JavascriptExecutor jse= (JavascriptExecutor) driver;
jse.executeScript("history.go(0)");

go(index) is used to navigate back and forward as well using negative and positive indexes respectively. go(-1) will navigate back while go(1) will navigate to forward if any history.

using reload() method:
WebDriver driver= new ChromeDriver();
JavascriptExecutor jse= (JavascriptExecutor) driver;
jse.executeScript("location.reload()");

So, we have seen many ways of refreshing a webpage. I will advise stick to refresh() method as it will work for sure always. Keep other methods in mind for interview purpose only.

That’s it guys. I hope you will have a better idea of ways of refreshing a webpage after reading this post.

If you like my posts, please like, comment, share and subscribe. Feedback and suggestions are always welcomed.

4 thoughts on “All Ways Of Refreshing a Webapge In Selenium

  1. Hi Amod Sir, While using actions class am getting this error “Exception in thread “main” java.lang.IllegalArgumentException: Key Down / Up events only make sense for modifier keys.”. Please look into the issue. Thank you.

Leave a Reply

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