186

I'm looking for more elegant way to refresh webpage during tests (I use Selenium2). I just send F5 key but I wonder if driver has method for refreshing entire webpage Here is my code

    while(driver.findElements(By.xpath("//*[text() = 'READY']")).size() == 0 )
        driver.findElement(By.xpath("//body")).sendKeys(Keys.F5);
        //element appear after text READY is presented      
    driver.findElement(By.cssSelector("div.column a")).click();    

Maybe is some better solution for finding element on manually refreshed page

13 Answers 13

342

In Java or JavaScript:

driver.navigate().refresh();

This should refresh page.

4
  • Glad! it helped you. Alternatively if you are using purely selenium RC or a WebDriverBackedSelenium you can also use: selenium.refresh(); Commented Apr 23, 2012 at 18:32
  • 60
    It's not exactly equivalent to pressing F5 though. driver.navigate().refresh() in its request header says "no-cache" and, as a result, unconditionally reloads all content. Whereas pressing F5 could result in a "If-Modified-Since" request, that could get a "304 Not Modified" response. You have to keep that difference in mind, if you do load testing. Commented Feb 16, 2013 at 15:49
  • @Manpreet Thanks for Solution. It's worthy to Upvote.
    – RNS
    Commented May 4, 2017 at 12:21
  • Why is there not a simple solution like this that does not send the "no-cache" header?
    – bdrx
    Commented Sep 21, 2023 at 15:42
94

In Python there is a method for doing this: driver.refresh(). It may not be the same in Java.

Alternatively, you could driver.get("http://foo.bar");, although I think the refresh method should work just fine.

1
  • 6
    If you are using driver.get("http://foo.bar") might as well just use driver.get(driver.current_url) Commented Apr 20, 2018 at 13:02
72

In python

Using built in method

driver.refresh()

or, executing JavaScript

driver.execute_script("location.reload()")
1
  • How we can refresh modal pages? Commented Jan 26, 2019 at 6:47
30

5 different ways to refresh a webpage using Selenium Webdriver

There is no special extra coding. I have just used the existing functions in different ways to get it work. Here they are :

  1. Using sendKeys.Keys method

    driver.get("https://accounts.google.com/SignUp");
    driver.findElement(By.id("firstname-placeholder")).sendKeys(Keys.F5);
    
  2. Using navigate.refresh() method

    driver.get("https://accounts.google.com/SignUp");  
    driver.navigate().refresh();
    
  3. Using navigate.to() method

    driver.get("https://accounts.google.com/SignUp");  
    driver.navigate().to(driver.getCurrentUrl());
    
  4. Using get() method

    driver.get("https://accounts.google.com/SignUp");  
    driver.get(driver.getCurrentUrl());
    
  5. Using sendKeys() method

    driver.get("https://accounts.google.com/SignUp"); 
    driver.findElement(By.id("firstname-placeholder")).sendKeys("\uE035");
    
0
21

Found various approaches to refresh the application in selenium :-

1.driver.navigate().refresh();
2.driver.get(driver.getCurrentUrl());
3.driver.navigate().to(driver.getCurrentUrl());
4.driver.findElement(By.id("Contact-us")).sendKeys(Keys.F5); 
5.driver.executeScript("history.go(0)");

For live code, please refer the link http://www.ufthelp.com/2014/11/Methods-Browser-Refresh-Selenium.html

1
  • 1
    Nice! multiple way to do it. findElement(By.id("Contact-us")) is not a good way. more reliable always present element is good, eg: '/html/body' . Does this work for hard refresh? and what if page has post data, firefox is known to show post data dialog.
    – mrtipale
    Commented Jan 30, 2018 at 10:50
12

Here is the slightly different C# version:

driver.Navigate().Refresh();
8

Alternate for Page Refresh (F5)

driver.navigate().refresh();

(or)

Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
5

One important thing to note is that the driver.navigate().refresh() call sometimes seems to be asynchronous, meaning it does not wait for the refresh to finish, it just "kicks off the refresh" and doesn't block further execution while the browser is reloading the page.

While this only seems to happen in a minority of cases, we figured that it's better to make sure this works 100% by adding a manual check whether the page really started reloading.

Here's the code I wrote for that in our base page object class:

public void reload() {
    // remember reference to current html root element
    final WebElement htmlRoot = getDriver().findElement(By.tagName("html"));

    // the refresh seems to sometimes be asynchronous, so this sometimes just kicks off the refresh,
    // but doesn't actually wait for the fresh to finish
    getDriver().navigate().refresh();

    // verify page started reloading by checking that the html root is not present anymore
    final long startTime = System.currentTimeMillis();
    final long maxLoadTime = TimeUnit.SECONDS.toMillis(getMaximumLoadTime());
    boolean startedReloading = false;
    do {
        try {
            startedReloading = !htmlRoot.isDisplayed();
        } catch (ElementNotVisibleException | StaleElementReferenceException ex) {
            startedReloading = true;
        }
    } while (!startedReloading && (System.currentTimeMillis() - startTime < maxLoadTime));

    if (!startedReloading) {
        throw new IllegalStateException("Page " + getName() + " did not start reloading in " + maxLoadTime + "ms");
    }

    // verify page finished reloading
    verify();
}

Some notes:

  • Since you're reloading the page, you can't just check existence of a given element, because the element will be there before the reload starts and after it's done as well. So sometimes you might get true, but the page didn't even start loading yet.
  • When the page reloads, checking WebElement.isDisplayed() will throw a StaleElementReferenceException. The rest is just to cover all bases
  • getName(): internal method that gets the name of the page
  • getMaximumLoadTime(): internal method that returns how long page should be allowed to load in seconds
  • verify(): internal method makes sure the page actually loaded

Again, in a vast majority of cases, the do/while loop runs a single time because the code beyond navigate().refresh() doesn't get executed until the browser actually reloaded the page completely, but we've seen cases where it actually takes seconds to get through that loop because the navigate().refresh() didn't block until the browser finished loading.

1
  • The answer with true value... Thanks.
    – Ogmios
    Commented Oct 25, 2018 at 9:04
3

You can also try

Driver.Instance.Navigate().Refresh();
2

There is also a case when you want to refresh only specific iframe on page and not the full page.

You do is as follows:

public void refreshIFrameByJavaScriptExecutor(String iFrameId){
        String script= "document.getElementById('" + iFrameId+ "').src = " + "document.getElementById('" + iFrameId+ "').src";
       ((IJavaScriptExecutor)WebDriver).ExecuteScript(script);
}
2

In PHP:

$driver->navigate()->refresh();
1

Another way to refresh the current page using selenium in Java.

//first: get the current URL in a String variable
String currentURL = driver.getCurrentUrl(); 
//second: call the current URL
driver.get(currentURL); 

Using this will refresh the current page like clicking the address bar of the browser and pressing enter.

2
  • does using getCurrentUrl() or get(CurrentUrl) refreshes the page? @RyanskyHeisenberg Commented Sep 16, 2020 at 11:51
  • 1
    No, getCurrentUrl() is for extracting the active URL only. You will need to call it again using driver.get(var_extractedURL). Basically it simulates refresh by copying the active URL and opening it again using the same browser's tab. I hope this helps. Commented Sep 21, 2020 at 9:21
1

In R you can use the refresh method, but to start with we navigate to a url using navigate method:

remDr$navigate("https://...")
remDr$refresh()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.