Refreshing Web Page By WebDriver When Waiting For Specific Condition

SeleniumRefreshWebdriverSelenium Webdriver

Selenium Problem Overview


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

Selenium Solutions


Solution 1 - Selenium

In Java or JavaScript:

driver.navigate().refresh();

This should refresh page.

Solution 2 - Selenium

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.

Solution 3 - Selenium

In python

Using built in method

driver.refresh()

or, executing JavaScript

driver.execute_script("location.reload()")

Solution 4 - Selenium

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");
    

Solution 5 - Selenium

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

Solution 6 - Selenium

Here is the slightly different C# version:

driver.Navigate().Refresh();

Solution 7 - Selenium

Alternate for Page Refresh (F5)

driver.navigate().refresh();

(or)

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

Solution 8 - Selenium

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.

Solution 9 - Selenium

You can also try

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

Solution 10 - Selenium

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);
}

Solution 11 - Selenium

In PHP:

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

Solution 12 - Selenium

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.

Solution 13 - Selenium

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()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpbaranskiView Question on Stackoverflow
Solution 1 - SeleniumManpreet SinghView Answer on Stackoverflow
Solution 2 - SeleniumIsaacView Answer on Stackoverflow
Solution 3 - SeleniumMd. Nazmul Haque SarkerView Answer on Stackoverflow
Solution 4 - SeleniumSandeepView Answer on Stackoverflow
Solution 5 - SeleniumAugustRushView Answer on Stackoverflow
Solution 6 - SeleniumdmeehanView Answer on Stackoverflow
Solution 7 - SeleniumPrashanth SamsView Answer on Stackoverflow
Solution 8 - SeleniummacView Answer on Stackoverflow
Solution 9 - Seleniumuser5320606View Answer on Stackoverflow
Solution 10 - SeleniumJohnnyView Answer on Stackoverflow
Solution 11 - SeleniumMegaCasperView Answer on Stackoverflow
Solution 12 - SeleniumRyanskyHeisenbergView Answer on Stackoverflow
Solution 13 - SeleniumTiago Martins PeresView Answer on Stackoverflow