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.

1 comment:

  1. hi i want to like this
    In selenium:
    if(selenium.isElementPresent("")
    {
    //actions
    }
    In webdriver:
    driver.findElement(By.id("")).isSelected or Enabled or Displayed)
    {
    //actions
    }

    which i can use select or displayed or enabled

    Plz inform to me. It's urgent.

    ReplyDelete

Note: Only a member of this blog may post a comment.