How to Refresh a Page in Selenium

Well, that sounds simple, right? How to Refresh a Page in Selenium? But did you know there are different ways to do that? I have written this post to show you all the possible ways that we have to refresh your webpage.

meme on page refresh

The first one is, you guessed right:

Using Navigate Refresh() method

You can simply make use of the following method whenever you wish to refresh your page:

driver.navigate().refresh();

This was discussed earlier in the Selenium Commands tutorial. You can go check it out to get a proper grasp on the basics of Selenium.

There are other ways you could refresh a page as well. Remember these are workarounds for refresh that basically do the same thing. However, it is recommended that you use the above-mentioned one for it’s a dedicated method created to avoid confusions.

Using the Send Keys.Keys Method

How do you generally refresh a web page with your keyboard? Isn’t that by simply pressing the shortcut F5 key?

We can leverage that fact by automating it using Send Keys. This is the way to do it:

driver.findElement(By.id(locator)).sendKeys(Keys.F5);

where locator needs to be an id on the webpage.

An example would be:

driver.findElement(By.id("gsr").sendKeys(Keys.F5);

Using the Send Keys Method

An alternative to the Send Keys.Keys method is when you use Send Keys Method.

An example is:

driver.findElement(By.id(locator)).sendKeys("\uE035");

where uE035 is the ASCII value of F5 key.

Refresh a Page Using Robot Class

Using the same logic of pressing the F5 key, we can make use of Robot class to achieve refresh.

Just type in the following piece of code, and it will press the F5 button from your keyboard:

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_F5);
robot.keyRelease(KeyEvent.VK_F5);

You might have to surround it using a try-catch block since it throws AWTException.

Using the Navigate To Method

Another simple way to refresh the page is using navigate.to() method.

Here’s the syntax that you could use:

driver.navigate.to(driver.getCurrentUrl);

where driver.getCurrentUrl returns the same URL instance on which you are on.

Using the Get Method to Refresh in Selenium

Another easy way to refresh the same page over is to reload it. How do you reload it? By providing the same URL to navigate to.

Simply use the following code to refresh the page:

driver.get(driver.getCurrentUrl());

What it does is use the current URL instance and passes it through get as a parameter.

As a result, the same page gets reloaded.

Using Javascript to Page Refresh

You can also refresh a page by taking the assistance of Javascript.

You need to cast your Web Driver first using JavascriptExecutor in order to do that. Here’s how:

JavascriptExecutor jse = (JavascriptExecutor) driver;

Once this is done you can use the instance jse that was created above to execute any javascript. The script to refresh a page would be:

History Go Method

jse.executeScript("history.go(0)");

You can simply test all of these examples on the website of Google.

meme refresh

Location Reload Method

Alternatively, you can use the following javascript to reload the page. It is called the location reload method:

jse.executeScript("location.reload()");

Are there any other ways to refresh a page in selenium? Please tell us in the comments section below.

Don’t forget to check out more cool tutorials on our website.

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

Leave a Reply