I am facing an issue with random behavior for element wait/present/clickable. I have used below logic to wait for that element but its working some times and does not working some other times. Can some one please help me how to solve this issue with generic/standard way of element wait.
public void isElementClickable(WebDriver driver, WebElement webelement,long timeOutForEachElement) {
try {
WebDriverWait wait = new WebDriverWait(driver,timeOutForEachElement);
wait.until(ExpectedConditions.elementToBeClickable(webelement));
} catch (UnhandledAlertException ex) {
//Some debug logging info
}
}
2)
public void waitTillElementPresent(WebElement webelement) {
System.out.println("Before Fluent Wait");
System.out.println("Xpath = " + webelement);
FluentWait<WebElement> fluentWait = new FluentWait<WebElement>(webelement);
fluentWait.pollingEvery(pollTime, TimeUnit.MILLISECONDS);
fluentWait.withTimeout(timeOut, TimeUnit.MILLISECONDS);
System.out.println("After Fluent Wait");
fluentWait.until(new Function<WebElement, Boolean>() {
public Boolean apply(WebElement webelement) {
try {
System.out.println("inside isDisplayed check");
return webelement.isDisplayed();
} catch (NoSuchElementException ex) {
System.out.println("Inside NoSuchElementException");
return false;
} catch (ElementNotVisibleException ex) {
System.out.println("Inside ElementNotVisibleException");
return false;
} catch (StaleElementReferenceException ex) {
System.out.println("Inside StaleElementRefException");
return false;
} catch (UnhandledAlertException ex) {
System.out.println("Inside UnhandledAlertException");
//Some logic for debug logging
return false;
}
}
});
}
3)
public void clickOnElement(String element){
while(isElementPresent(element))
{
driver.findElement(By.xpath(element)).click();
System.out.println("Trying to click on element"+element);
break;
}
System.out.println("Clicked on element"+element);
}
4)
public void waitTillElementisClicked(String element){
boolean flag = true;
while(flag==true){
//driver.findElement(By.xpath(element)).click();
//flag=driver.findElement(By.xpath(element)).isSelected();
driver.findElement(By.xpath(element)).click();
// driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
if(driver.findElement(By.xpath(element)).isSelected()){
flag=false;
System.out.println("element already clicked");
}
// driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
System.out.println("trying to click the element");
}
System.out.println("Clicking the element");
}
Thanks & Regards,
krishnareddy.K