Refreshing a page might seem simple. But when you're working with dynamic applications, forms, or session-sensitive flows, the way you refresh matters. A mis-timed reload can cause flaky results or unexpected behavior in your automation scripts.
That’s why knowing how to refresh the page in Selenium is essential. There are multiple ways to reload a page, and each has its own use case. Some simulate user behavior. Others trigger a full re-render of the DOM. Your choice affects both speed and test stability.
In this article, we’ll walk you through:
If you’ve ever asked, “What’s the best way to reload a page without breaking the test?”, you’ll find your answer here.
Let’s get started.
There are many reasons you might want to refresh a page while using Selenium WebDriver. Each case comes from real user behavior and helps validate application stability.
This is the most direct way to refresh a web page in Selenium. When you use driver.navigate().refresh(), Selenium simulates a browser refresh, just like when a user clicks the refresh button or presses F5.
It is fast, consistent, and fits perfectly in use cases where a standard reload is required. You can use this approach for AJAX retry testing, page stability checks, or refreshing after a login session timeout.
The method does not reload the page from the cache. Instead, it fully reloads the current document, making it suitable for dynamic sites.
The following code shows how to refresh the page in Selenium using navigate().refresh().
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class RefreshExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://katalon.com");
// Refresh the current page
driver.navigate().refresh();
driver.quit();
}
}
In Selenium, driver.get(driver.getCurrentUrl()) reloads the current page by navigating to the same URL again. It mimics typing the address into the browser bar and pressing enter.
This is slightly slower than navigate().refresh() but it guarantees a full page reload. You can use it when you want to clear session states or test how an application behaves after a complete page reload.
This method is especially helpful for verifying cache behavior or for reloading pages that load scripts dynamically.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class HardRefreshExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://katalon.com");
// Hard reload using the current URL
String currentUrl = driver.getCurrentUrl();
driver.get(currentUrl);
driver.quit();
}
}
This approach uses the navigation API to move to the current URL again. It works just like driver.get() but supports history-based navigation, which fits well in end-to-end flows where the browser moves back and forth between pages.
In cases where your test jumps through multiple pages and then reloads the current one, this method feels more aligned with real user behavior. It also supports more advanced navigation strategies like chaining refresh with backward or forward steps.
If you're exploring how to refresh the page in Selenium for a journey-driven application, this method is a great choice.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class NavigationRefreshExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://katalon.com");
// Reload using navigation API
String currentUrl = driver.getCurrentUrl();
driver.navigate().to(currentUrl);
driver.quit();
}
}
If you want more control over how you refresh the page, the JavaScript path gives you flexibility. It directly invokes the browser's native reload function, just like a developer would do in the browser console.
You can use this approach to trigger either a soft reload or a hard reload. By default, it performs a soft reload, which uses cache. You can add a true parameter to perform a full reload that bypasses cache and reloads from the server.
This is a really good method when you're testing dynamic components that rely heavily on AJAX calls like stock tickers or live feeds. You can trigger the refresh at exactly the right time and even chain it with other JavaScript-based validations.
Browser behavior might vary slightly depending on the app framework. It's a great technique when native reload methods don't fully update the DOM.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.JavascriptExecutor;
public class JavaScriptRefreshExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://katalon.com");
// Refresh using JavaScript
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("location.reload()");
driver.quit();
}
}
This method is ideal when your goal is to simulate exactly what a user does in the browser. If you are testing keyboard-driven behavior or accessibility scenarios, this is a perfect fit. It sends real keystrokes to the browser window, like pressing F5 or Ctrl+R.
You can use the Actions class to send key inputs. On Windows or Linux, F5 and Ctrl+R work well. For macOS, you can simulate Command+R. Choose based on your platform to ensure consistency.
It fits well in UI-heavy applications where you want a full page refresh while preserving real-world interactions.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.Keys;
public class KeyboardRefreshExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://katalon.com");
// Refresh using keyboard F5
Actions actions = new Actions(driver);
actions.sendKeys(Keys.F5).perform();
driver.quit();
}
}
Method | How It Works | Speed / Overhead | Best For |
---|---|---|---|
driver.navigate().refresh() | Simulates a browser refresh (like clicking the refresh button or pressing F5). | Fast, low overhead. | Standard reloads, AJAX retry, session timeout checks. |
driver.get(driver.getCurrentUrl()) | Navigates to the current URL again, forcing a full reload. | Slightly slower than navigate().refresh(). | Clearing session states, testing cache behavior. |
driver.navigate().to(driver.getCurrentUrl()) | Uses the navigation API to go to the same URL, fits well with history-based flows. | Similar speed to driver.get(). | Journey-driven apps, chaining refresh with back/forward steps. |
JavaScript Executor (location.reload) | Directly executes JS to reload the page, can perform soft or hard reload (true parameter bypasses cache). | Flexible but depends on browser behavior. | Dynamic components, AJAX-heavy pages, precise timing control. |
Keyboard Shortcuts (F5 / Ctrl+R) | Sends keystrokes to simulate user pressing F5 or Ctrl+R (Command+R on macOS). |
Slowest, but most realistic. |
Accessibility tests, simulating real user keyboard input. |
Katalon is a low-code automation platform built on top of Selenium. It brings a modern interface and powerful built-in capabilities that remove the complexity of traditional test scripts, making test automation more accessible to teams of all skill levels.
With Katalon, you get:
In short, Katalon supercharges Selenium. It gives you the power of test automation in a scalable, team-friendly package that grows with your projects.
📝 Want to explore what Katalon can do for your team? Request a demo to see how it helps teams automate faster with less effort.