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.

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

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 ctrl+R

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:

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

Using JavaScripts:

using go() method:

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:

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.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Writing technical posts and creating YouTube videos are my hobbies.

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

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.