Refreshing web page by WebDriver when waiting for specific condition.

SeleniumAutomation TestingTesting Tools

We can refresh a web page by Selenium webdriver when waiting for a specific condition. We can take the help of the driver.navigate().refresh() method to refresh the webpage.

Example

Code Implementation with refresh().

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BrowserRefresh{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.tutorialspoint.com/index.htm");
      // refresh webpage with refresh method
      driver.navigate().refresh();
   }
}

We can refresh a webpage with the help of sendKeys method and then send Keys.F5 as an argument to the method. The sendKeys method is capable of accepting Keys interaction. We have to add import org.openqa.selenium.Keys statement to the code to utilize the Keys class.

Example

Code Implementation with sendKeys().

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.Keys;
public class BrowserKeys{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.tutorialspoint.com/index.htm");
      // refresh webpage with sendKeys method
      driver.findElement(By.id("gsc-i-id1")).sendKeys(Keys.F5);
   }
}

We can obtain the currently visited URL with the help of the getCurrentUrl method. We can also refresh the web page with the help of the driver.navigate().to() method and pass the value returned from the getCurrentUrl method as an argument to that method.

Example

Code Implementation with navigate().to().

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BrowserRefreshNavigate{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.tutorialspoint.com/index.htm");
      // refresh webpage with navigate method
      driver.navigate().to(driver.getCurrentUrl());
   }
}

We can refresh the webpage with the help of the get method and pass the URL to be launched as an argument to the method. Once a web page is opened, we can reload the page by hitting the same URL again.

Example

Code Implementation with get().

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BrowserRefreshGet{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.tutorialspoint.com/index.htm");
      // refresh webpage with get method
      driver.get(driver.getCurrentUrl());
   }
}

We can also refresh a web page with the help of Javascript Executor . To run theJavascript command, we shall take the help of the executeScript  method and pass history.go(0)  Javascript command as an argument to the method.

Example

Code Implementation with Javascript Executor.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.JavascriptExecutor;
public class BrowserRefreshJs{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.tutorialspoint.com/index.htm");
      // refresh webpage with executeScript method
      driver.executeScript("history.go(0)");
   }
}
raja
Published on 26-Oct-2020 06:50:11
Advertisements