181

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

Deepak Rai
  • 2,163
  • 3
  • 21
  • 36
pbaranski
  • 22,778
  • 19
  • 100
  • 117

13 Answers13

330

In Java or JavaScript:

driver.navigate().refresh();

This should refresh page.

Jeremy Moritz
  • 13,864
  • 7
  • 39
  • 43
Manpreet Singh
  • 3,643
  • 2
  • 18
  • 14
  • Glad! it helped you. Alternatively if you are using purely selenium RC or a WebDriverBackedSelenium you can also use: selenium.refresh(); – Manpreet Singh Apr 23 '12 at 18:32
  • 57
    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. – Vsevolod Golovanov Feb 16 '13 at 15:49
  • @Manpreet Thanks for Solution. It's worthy to Upvote. – RNS May 04 '17 at 12:21
90

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.

Isaac
  • 9,372
  • 4
  • 28
  • 31
69

In python

Using built in method

driver.refresh()

or, executing JavaScript

driver.execute_script("location.reload()")
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");
    
ono2012
  • 4,967
  • 2
  • 33
  • 42
Sandeep
  • 877
  • 1
  • 9
  • 16
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

AugustRush
  • 367
  • 4
  • 4
  • 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 Jan 30 '18 at 10:50
12

Here is the slightly different C# version:

driver.Navigate().Refresh();
dmeehan
  • 2,317
  • 2
  • 26
  • 31
8

Alternate for Page Refresh (F5)

driver.navigate().refresh();

(or)

Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
Prashanth Sams
  • 19,677
  • 20
  • 102
  • 125
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.

mac
  • 2,672
  • 4
  • 31
  • 43
3

You can also try

Driver.Instance.Navigate().Refresh();
josliber
  • 43,891
  • 12
  • 98
  • 133
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);
}
Johnny
  • 14,397
  • 15
  • 77
  • 118
2

In PHP:

$driver->navigate()->refresh();
MegaCasper
  • 1,871
  • 1
  • 13
  • 12
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.

ono2012
  • 4,967
  • 2
  • 33
  • 42
  • does using getCurrentUrl() or get(CurrentUrl) refreshes the page? @RyanskyHeisenberg – learningIsFun Sep 16 '20 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. – RyanskyHeisenberg Sep 21 '20 at 09: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()
Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145