Vidushi
Vidushi

Reputation: 15

Element.Disabled throws error as element not found

for my test I need this element.Disabled to return false.

When test reaches to a page where that element is not displayed , test gets failed with element not found.

below code fails as username textbox doesnot display on the page

if (VerifyUsernameTextBox())
{
do something
}

public bool VerifyUsernameTextBox()
        {
            return username.Displayed;  // code fails with no element found while return false is expected .

}

What to do so I get element.Displayed as false .

Upvotes: 0

Views: 145

Answers (1)

Don Lee
Don Lee

Reputation: 463

You find the element by driver.findElement(), right? Remember, findElement throws an exception if it doesn't find an element, so you need to properly handle it.

You can do like this:

    private bool IsUsernamePresent(By by)
    {
        try
        {
            driver.findElement(by);
            return true;
        }
        catch (NoSuchElementException)
        {
            return false;
        }
    }

then

public bool VerifyUsernameTextBox()
{
    return IsUsernamePresent(By.Id("element_username_id"));
}           

Upvotes: 2

Related Questions