We can refresh the page in many ways. Here we will discuss some of the ways.
Using refresh() method:
In Java,
driver.navigate().refresh();
Using sendkeys() method:
sendkeys() method is used like we do page refresh pressing F5 key through our keyboard. This manual task is executed by code using the sendkeys() command over an element.
The command will be like
driver.findElement(By.id(locator)).sendKeys(F5 key);
In Java,
driver.findElement(By.id("gbqfq")).sendKeys(Keys.F5);
Using navigate().to() method:
Actually browsing the same URL using navigate().to() function. We call getCurrentUrl() function to get the current URL of the page.
In Java,
Using get() method:driver.navigate().to(driver.getCurrentUrl());
If we know the URL then we can load the same URL again to reload the page. In Java,
driver.get("https://www.google.com.bd/");
We can also use getCurrentUrl() function to know the current URL of the page. The command would be, driver.get(driver.getCurrentUrl());
Using sendkeys() method with ASCII code:
We can use ASCII code as argument on sendkeys() method that is equivalent to F5 key command of the keyboard.
driver.findElement(By.id("gbqfq")).sendKeys("\uE035");
Here \uE035 is the ASCII code of F5 key.
Using executeScript() method:
Using this command we can execute any JavaScript for our need. If we execute location.reload() JavaScript function then current page will be reloaded which meets our purpose.
The command is,
driver.executeScript("location.reload()");
There are many other ways but these are the generally used ways. I think this will help us.
Happy Selenium Webdrivering!!!
Thanks for providing good article on "How to refresh or reload a page in selenium webdriver". http://reditblog.blogspot.in/
ReplyDeleteCan anyone tell me how to refresh browser window using javascript in selenium..
ReplyDeleteThanks
ReplyDeletedriver.executeScript("location.reload()");
ReplyDeletethis one is not working, it asks add cast to driver.