Showing posts with label isElementPresent. Show all posts
Showing posts with label isElementPresent. Show all posts

Saturday, July 14, 2012

isElementPresent ?

Selenium RC we became quite used to using methods like iSElementPresent and waitForCondition/waitForPageToLoad to deal with Ajax and page loading events. With WebDriver we have to write them ourselves.


There are 4 important things going on here. In order:
  1. Setting implicity_wait to 0 so that WebDriver does not implicitly wait.
  2. Returning True when the element is found.
  3. Catching the NoSuchElementException and returning False when we discover that the element is not present instead of stopping the test with an exception.
  4. Setting implicitly_wait back to 10 after the action is complete so that WebDriver will implicitly wait in future.
Method
 isElementPresent(WebDriver driver,By by)  
 {  
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);  
    try  
    {  
       driver.findElement(by);  
       return true;  
    }  
    catch(Exception e)  
    {  
       return false;  
    }  
    finally  
    {  
       driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);  
     }  
 }  


Regards,
Santosh.