4 Ways to Refresh Page In Selenium Webdriver

In this tutorial, we will learn four different ways to refresh the page in Selenium Webdriver. While automating a web application there may be certain cases wherein you may require to refresh the page. The following scenarios are a few examples.

  • Imagine you are opening a page in Selenium but all the elements are not being loaded in the initial page load and it requires one more refresh to load all the elements.
  • You have updated certain things on the page and want to verify the updated content after refreshing the content.

Different ways to refresh page in Selenium Webdriver

Fortunately, Selenium does have the refresh command to refresh the browser page. The following are different ways to refresh page in Selenium and we will explore them one by one.

  1. driver.navigate().refresh() command
  2. driver.get(driver.getCurrentUrl()) command
  3. Keyboard events or Send Keys method
  4. driver.navigate.to(driver.getCurrentURL()) command

Examples of Refresh Page in Selenium WebDriver

driver.navigate().refresh() Command

We can use this Selenium inbuilt method for performing page refresh operations. It is the most preferred way to refresh a web page. We can use the following syntax to refresh a web page.

driver.navigate().refresh();

This refresh method of the Navigation interface neither requires any arguments nor returns any value.

Test Scenario

  1. Launch the browser
  2. Open https://www.google.com/
  3. Maximize the page
  4. Once the page is loaded successfully, refresh the web page

driver.get(driver.getCurrentUrl()) Command

It is a way to refresh a page in the recursive way wherein we open the current URL in the currently open page. The driver.getCurrentUrl() command retrieves the currently opened URL and driver.get() command again opens the same URL, this is how it works. The syntax is as follows.

driver.get(driver.getCurrentUrl());

Test Scenario

  1. Launch the browser
  2. Open https://www.google.com/
  3. Maximize the page
  4. Again open https://www.google.com/ on the currently opened page to refresh it

Refresh Page in Selenium Using Keyboard Events or Send Keys method

If you have to manually refresh a page using keyboard event what you will do, obviously you will press F5 Key on the keyboard. The same is also applicable to Selenium and the widely used method. In order to get it worked, you will have to first identify a valid web element on the web page and then use the send keys method. The syntax is as follows.

driver.get(“https://www.google.com”);
driver.findElement(By.name(“q”)).sendKeys(Keys.F5);

Test Scenario

  1. Launch the browser
  2. Open https://www.google.com/
  3. Maximize the page
  4. Locate search textbox and press F5 key

driver.navigate.to(driver.getCurrentURL()) Command

We can also use to() method of the Navigation interface to navigate to a particular URL. It requires a URL as a String argument and it returns nothing. The syntax is as follows.

driver.get(“https://www.google.com”);
driver.navigate().to(driver.getCurrentUrl());

Conclusion

We have learned different ways of refreshing a web page in Selenium WebDriver.You can use any of them depending on the situation.Hope you have liked this article.Please don’t forget to share it.

Recommended Posts

Leave a Reply