free amp templates

Wait Function for checking the Web Element's visibility

WebDriverWait wait = new WebDriverWait(driver, 60);
 
public boolean isElementLocated(WebElement element) throws Exception {
 try {
  wait.until(ExpectedConditions.visibilityOf(element));
  return true;
 } catch (Exception e) {
  System.err.println("Web element isn't identified!!");
  return false;
 }
}
 
public static void main(String[] args) throws Exception{
 WebElement icon = driver.findElement(By.id("icon"));
 if(isElementLocated(icon) == true){
  icon.click();
 }
}

Pass the Web element to “isElementLocated” method in main function and check, it will wait for the presence of web element up to 60 secs.
Because when we are using “thread.sleep”, sometimes we could not find the element within that time period, for that we can use this “isElementLocated” method.
It will wait for the location of the element to be clickable upto some seconds which you mentioned.

SHARE THIS PAGE!