Refresh Page using Selenium Webdriver

โšก Smart Summary

Refreshing a page in Selenium WebDriver reloads the current URL so that every element renders before assertions run, and the framework offers five distinct commands that achieve the same reload in different ways.

  • ๐Ÿ”˜ Default choice: driver.navigate().refresh() is the built-in reload and takes no arguments and returns no value.
  • โ˜‘๏ธ Recursive variants: Passing getCurrentUrl() into get() or navigate().to() reloads the same address.
  • โœ… Keyboard route: sendKeys accepts Keys.F5 or the Unicode value \uE035, but it must target a real element.
  • ๐Ÿงช Script route: JavascriptExecutor running location.reload() works when the WebDriver command is blocked.
  • ๐Ÿ› ๏ธ After the reload: Every element reference captured earlier becomes stale and has to be located again.
  • ๐Ÿ“Š Modern setup: Selenium Manager arrived in version 4.6, so the driver path no longer has to be set by hand.

Refresh page using Selenium WebDriver commands

During test automation of a web-based application, there comes a need for the page to be refreshed multiple times for all web elements to be loaded completely. On the initial page load some web elements might be loaded, while it takes a second page refresh for all of them to appear. This can be done using the refresh command provided by Selenium WebDriver.

How to Refresh Page in Selenium

Browser refresh operation can be performed using the following ways in Selenium. We will discuss the below mentioned ways in detail throughout the article.

  1. driver.navigate().refresh() command
  2. Get method
  3. Send Keys command
  4. Navigate method
  5. driver.navigate().to() command

1) driver.navigate().refresh() Command

This is the inbuilt method for performing a page refresh operation provided by Selenium WebDriver. This command is the most commonly used command across test automation for performing a page refresh operation. The refresh command can be used in a simple way as mentioned below.

driver.get("https://demo.guru99.com/selenium/guru99home/");
driver.navigate().refresh();

Navigation is an interface that is used to perform various browser operations such as navigating to the previous page, navigating to the next page, page refresh and browser close. Navigation interface methods can be accessed using the command driver.navigate(). The refresh method of the Navigation interface does not take any arguments and does not return any value.

Example:

Test Scenario:

  1. Open the Chrome browser with the web page https://demo.guru99.com/selenium/guru99home/
  2. Once the page is loaded successfully, refresh the web page using the driver.navigate().refresh() method
  3. Close the browser using the driver.close() method

Code

package Guru99Demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class RefreshDemo {
public static void main(String args[]) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://demo.guru99.com/selenium/guru99home/");
driver.manage().window().maximize();
driver.navigate().refresh();
driver.close();
}
}

โš ๏ธ Version note: Selenium Manager shipped with Selenium 4.6 and downloads the matching browser driver automatically, so the System.setProperty line above is no longer required on a current version โ€” new ChromeDriver() is enough. The original line is kept because it is still correct for the Selenium 3 style setup the example was written against.

Code Output:

On the initial page load the demo home page renders as shown below.

Guru99 demo home page on the initial load before the refresh command runs

After the page refresh operation the video on the page has changed, which confirms the reload actually happened.

Guru99 demo home page after driver.navigate().refresh() showing a different video

Multiple Other Ways to Refresh a Page

The refresh method is not the only route to a reload. The four techniques below produce the same result and are worth knowing, because a locked-down application or a stubborn browser occasionally blocks one of them.

2) Get Method

The get method can be used in a recursive way to refresh a page. In order to achieve this, we need to pass another method as an argument to the get method.

Example:

driver.get("https://www.guru99.com");
driver.get(driver.getCurrentUrl());

3) Navigate Method

This method uses the same concept of recursion as mentioned above. The getCurrentUrl() method is passed as an argument to the driver.navigate().to() method.

Example:

driver.get("https://www.guru99.com");
driver.navigate().to(driver.getCurrentUrl());

4) Send Keys Method Using the F5 Key

This is the second most commonly used method to refresh a page in Selenium. It takes the refresh key (F5) as an argument to the sendKeys method. Since sendKeys works only on web elements rather than on the browser, we must first identify a valid web element on the web page and then use the sendKeys method. This can be accomplished as shown below.

Example:

driver.get("https://www.guru99.com");
driver.findElement(By.id("username")).sendKeys(Keys.F5);

Because the keystroke is delivered to an element rather than to the browser chrome, this route depends on the element holding focus. If the locator matches a field that cannot take focus, the key is swallowed and the page never reloads.

5) Send Keys Method Using ASCII Code

This method uses the same concept as above, but instead of passing the F5 key as an argument we send the code point of the refresh key as an argument. This can be accomplished as shown below.

driver.get("https://www.guru99.com");
driver.findElement(By.id("username")).sendKeys("\uE035");

The value \uE035 is the WebDriver Unicode code point that the protocol maps to F5, so this snippet and the previous one are equivalent.

How to Refresh a Page Using JavaScriptExecutor

Every browser exposes a reload function to page scripts, and Selenium can call it through the JavascriptExecutor interface. This is the usual fallback when the WebDriver refresh command is intercepted by the application or by a browser extension.

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("location.reload();");

Two variations are worth remembering:

  1. location.reload() performs a normal reload and may serve the page from cache.
  2. history.go(0) is an older equivalent that some legacy suites still use.

The executor route runs inside the page, so it does not wait for the new document on its own. Follow it with an explicit wait exactly as you would after driver.navigate().refresh(), and remember that a script-driven reload will not clear a browser-level dialog. If the application under test loads its content asynchronously, the same care applies as when handling AJAX calls in Selenium WebDriver.

How to Avoid StaleElementReferenceException After a Refresh

A refresh destroys the current DOM and builds a new one. Every WebElement captured before the reload now points at a node that no longer exists, so the next call against it throws StaleElementReferenceException. This is the single most common failure that follows a page refresh.

Three habits remove almost all of these failures:

  1. Locate after, never before. Store the By locator rather than the WebElement, and call findElement again once the reload has finished.
  2. Wait for a real signal. An explicit wait on the visibility of an element that only exists after the reload is far more reliable than a fixed sleep.
  3. Refresh, then re-read. Never carry a text value or an attribute captured before the reload into an assertion made after it.

The pattern below applies all three:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
driver.navigate().refresh();
WebElement search = wait.until(
        ExpectedConditions.visibilityOfElementLocated(By.id("username")));
search.sendKeys("guru99");

WebDriverWait takes a Duration in Selenium 4, which is a change from the integer-seconds constructor used in Selenium 3. Wider techniques for these failures are covered in our guide to exception handling in Selenium.

Comparison of Selenium Page Refresh Methods

All six commands reload the page, but they differ in what they depend on and in how they fail.

Method Depends on Reloads via When it is the right choice
driver.navigate().refresh() Nothing extra The browser reload command Almost always โ€” the default
driver.get(driver.getCurrentUrl()) A readable current URL A fresh navigation When you also want to drop the history entry
driver.navigate().to(driver.getCurrentUrl()) A readable current URL A fresh navigation Same as above, written through the Navigation interface
sendKeys(Keys.F5) A focusable element A keystroke When you are deliberately testing the keyboard path
sendKeys(“\uE035”) A focusable element A keystroke Identical to F5, written as a code point
JavascriptExecutor location.reload() Scripting enabled A page script When the WebDriver command is blocked

Start with the refresh command, and reach for another row only when it fails for a reason you understand. The same order of preference applies when you maximize or resize the browser window: prefer the native WebDriver call and treat the script route as the fallback.

FAQs

It can. If the current page was produced by a POST, the browser may ask to resend the data and the reload stalls behind that dialog. Navigating to the URL again with get() avoids the prompt entirely.

The binding names differ but the command is the same: driver.refresh() in Python, driver.Navigate().Refresh() in C# and driver.navigate().refresh() in Java. All three issue the identical WebDriver instruction.

No. A reload keeps cookies, session storage and local storage for the same origin, which is why refresh is a poor way to reset state. Delete cookies explicitly or start a fresh browser session instead.

Self-healing engines re-resolve a locator from several attributes when the reloaded DOM no longer matches, and failure-analysis models cluster reload-timing flakes so the team fixes one wait rather than fifty tests.

Yes, and the one-line refresh call is a suggestion it gets right consistently. The wait that has to follow it is where suggestions go wrong, so review any generated timeout against how the application actually loads.

There is no dedicated command. Teams approximate one by sending Ctrl and F5 together through the Actions class, or by disabling the cache through browser options, which is the more dependable route.

Not with the refresh command, which always reloads the top document. Switch into the frame and reload it with a script, or reset its source attribute, then switch back to the default content.

Do not guess with a fixed sleep. Use an explicit wait of ten to thirty seconds on a condition that is only true after the reload, so the test continues the moment the page is ready.

Summarize this post with: