Having an issue with Wait till element present functionality in Selenium Web driver 2.44.0

44 views
Skip to first unread message

krishnareddy kommireddy

unread,
Jun 29, 2015, 5:15:01 PM6/29/15
to seleniu...@googlegroups.com, Sandeep Mn
Hi All,

 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.

Issue/Requirement : I have to wait for an element till its present/clickable while loading web page. I am using Selenium Web driver 2.44.0 and Firefox 33.0.3

Logic used : 

1)
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

KRISRAJ

unread,
Jun 29, 2015, 5:15:01 PM6/29/15
to seleniu...@googlegroups.com

Hi All,

 

 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.

 

Issue/Requirement : I have to wait for an element till its present/clickable while loading web page. I am using Selenium Web driver 2.44.0and Firefox 33.0.3

Mercious

unread,
Jul 1, 2015, 3:54:34 AM7/1/15
to seleniu...@googlegroups.com
I am quite disappointed with Seleniums built-in functions to wait for a certain condition myself.

I have handcrafted these and they work way more stable.

Basically what i am doing is:
Write a function that is passed a maximumRetryTime and a By-locator.

Let's say the function wants to wait until an element is clickable, so i do (PSEUDO-CODE)!!
function
  while(triedTime<maxiumRetryTime) 
    try finding the element
    try clicking the element
    return out of function with success
    catch NoSuchElementException
      continue
    catch ElementNotClickable/Related Exceptions
      continue
  end of while
  throw ExpiredWithoutSuccessException
end of function


You could add sleeps if you are afraid of overhead but in reality noone gives two shits about a while loop in an I/O heavy environment that Selenium operates in. 
Reply all
Reply to author
Forward
0 new messages