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.
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.
- driver.navigate().refresh() command
- Get method
- Send Keys command
- Navigate method
- 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:
- Open the Chrome browser with the web page https://demo.guru99.com/selenium/guru99home/
- Once the page is loaded successfully, refresh the web page using the driver.navigate().refresh() method
- 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.
After the page refresh operation the video on the page has changed, which confirms the reload actually happened.
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:
- location.reload() performs a normal reload and may serve the page from cache.
- 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:
- Locate after, never before. Store the By locator rather than the WebElement, and call findElement again once the reload has finished.
- 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.
- 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.


