Issue with isDisplayed() method

54 views
Skip to first unread message

chandaluri sudheer

unread,
Dec 8, 2014, 4:51:28 AM12/8/14
to seleniu...@googlegroups.com
Hi All,

I'm having an issue in web driver while using isDisplayed() method.

I need to check for a dynamic object existence. If the object exists, then I need to do some actions and if it doesn't exist, I need to perfrom some other actions.

When I used the method isDisplayed() in if condition, if the object is really displayed, then it is working fine. If the object doesn't exists on the screen, then instead of returning false it is throwing an exception.

Can anyone suggest be any other best way to handle this situation?

Sample Code:

if(driver.findElement(By.id(expID)).isDisplayed()) //I've tried with isEnabled as well but it is not working.
{
    driver.findElement(By.id(expID)).click();
    Thread.sleep(5000);
}
else
{
    //Do other actions
}

Many Thanks in advance.

PeterJeffreyGale

unread,
Dec 9, 2014, 4:37:00 AM12/9/14
to seleniu...@googlegroups.com
Check first for the object existence, and then use IsDisplayed() only if you know it exists.

Seems you are checking if a nin- existent object is displayed, though you could just handle the error to treat it as undisplayed.

Rajeev Prabhakaran Nair

unread,
Dec 9, 2014, 5:11:51 AM12/9/14
to seleniu...@googlegroups.com
public boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

can you try to check if the element is visible or not?


Regards / Rajeev
Reply all
Reply to author
Forward
0 new messages