JustToKnow
JustToKnow

Reputation: 823

Visible, enabled or selected element in Selenium

I'm a little confused and I dont really know what would it be the best way to go.

I'm trying to determine whether an element is visible, enabled or selected. I was thinking about these 3 options:

a)Boolean button = driver.findElement(By.id(localizadorId)).isDisplayed();
b)Boolean button = driver.findElement(By.id('localizadorId').isEnable();
c)Boolean button = driver.findElement(By.id("localizadorId")).isSelected();

Does it make sense to you? Which one would it be the best way to go and why?

Upvotes: 1

Views: 3830

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193218

isDisplayed(), isEnable() and isSelected() are three different methods to validate three distinct stage of a WebElement.


isDisplayed()

isDisplayed() validates if a certain element is present and displayed. If the element is displayed, then the value returned is true. If not, then the value returned is false. However this method avoids the problem of having to parse an element's style attribute. Example:

boolean eleDisplayed= driver.findElement(By.xpath("xpath")).isDisplayed();

isEnabled()

isEnabled() validates if an element is enabled. If the element is enabled, it returns a true value. If not, it returns a false value. This will generally return true for everything but disabled input elements. Example:

boolean eleEnabled= driver.findElement(By.xpath("xpath")).isEnabled();

isSelected()

isSelected() method is often used on radio buttons, checkboxes or options in a menu. It is used to determine is an element is selected. If the specified element is selected, the value returned is true. If not, the value returned is false. Example:

boolean eleSelected = driver.findElement(By.xpath("xpath")).isSelected();

Upvotes: 4

Related Questions