1

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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
JustToKnow
  • 785
  • 6
  • 23
  • Is it `or` or `and` that you are looking for? If it is `or`, then I'd say these elements are standalone as of now (each one is not connected to other, and let's say if the element is not visible, then the (a) would fail, and the code might not reach (b). Contrarily, if it is `and`, then it should be fine, for firs the element gets visible, then enabled, then selected. – Anand Gautam Feb 03 '22 at 17:59

1 Answers1

3

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();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352