Reputation: 25062
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
Upvotes: 189
Views: 445987
Reputation: 15632
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()
Upvotes: 1
Reputation: 53
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.
Upvotes: 1
Reputation: 977
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 :
Using sendKeys.Keys method
driver.get("https://accounts.google.com/SignUp");
driver.findElement(By.id("firstname-placeholder")).sendKeys(Keys.F5);
Using navigate.refresh() method
driver.get("https://accounts.google.com/SignUp");
driver.navigate().refresh();
Using navigate.to() method
driver.get("https://accounts.google.com/SignUp");
driver.navigate().to(driver.getCurrentUrl());
Using get() method
driver.get("https://accounts.google.com/SignUp");
driver.get(driver.getCurrentUrl());
Using sendKeys() method
driver.get("https://accounts.google.com/SignUp");
driver.findElement(By.id("firstname-placeholder")).sendKeys("\uE035");
Upvotes: 34
Reputation: 2734
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:
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.
Upvotes: 5
Reputation: 3771
In Java or JavaScript:
driver.navigate().refresh();
This should refresh page.
Upvotes: 343
Reputation: 2357
Here is the slightly different C# version:
driver.Navigate().Refresh();
Upvotes: 12
Reputation: 377
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
Upvotes: 21
Reputation: 15423
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);
}
Upvotes: 2
Reputation: 1051
In python
Using built in method
driver.refresh()
or, executing JavaScript
driver.execute_script("location.reload()")
Upvotes: 73
Reputation: 21169
Alternate for Page Refresh (F5)
driver.navigate().refresh();
(or)
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
Upvotes: 8
Reputation: 9682
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.
Upvotes: 94