How to refresh page in Selenium Java using WebDriver?

How to refresh the page in Selenium Java using WebDriver?

I’m working on automating browser actions using Selenium WebDriver and need to perform a page refresh during the test flow.

I want to know the correct and reliable way to refresh the current page in Selenium Java.

What method should I use, and are there any best practices or caveats to consider when using navigate().refresh() or similar approaches in Selenium Java?

Looking for a simple example or guidance on how to refresh the page in Selenium Java effectively?

Hey! I’ve run into the same need before when working with test cases that rely on refreshed content, especially after file uploads or session timeouts. The most straightforward way I’ve found is using:

driver.navigate().refresh();

It behaves just like hitting the browser’s refresh button. I usually wrap it with a small WebDriverWait to ensure the page has fully loaded again before continuing with the next steps.

It’s reliable, clean, and doesn’t require any complex handling unless your page has dynamic or lazy-loaded elements.

Hi there! In one of my earlier projects, I noticed that .navigate().refresh() didn’t always play nice with Angular apps.

What worked better for me was simulating a keyboard refresh using the Robot class in Java:

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

This approach mimics a real user pressing F5, so it triggers the same browser-level refresh.

Just a heads-up: this requires the browser to be in focus, so it’s more suitable for local or GUI-based executions, not headless CI environments.

Hey! When I needed a refresh in tests running on a flaky web app that had caching issues, I took a different route:

I just reloaded the current URL.

Like this:

driver.get(driver.getCurrentUrl());

I know it’s not the conventional refresh(), but in some cases, this method forced a cleaner reload of all resources, especially when dealing with outdated states.

It’s been surprisingly stable for me when testing login sessions or resetting forms between test steps. Definitely worth trying if you’re running into edge cases with .refresh().