Precisesly Selenium deals with three unique states of an element.
-
Presence of element within the html: This state of an element can be detected through the ExpectedCondition
presenceOfElementLocated()
where the expectation is to check if the element is present in the DOM of a page. This does not necessarily mean that the element is visible.-
Exmaple:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("css_of_element")));
-
-
Visibility of element within the html: This state of an element can be detected through the ExpectedCondition
visibilityOfElementLocated()
where the expectation is to check if the element is present in the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.-
Exmaple:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("css_of_element")));
-
-
Element to be clickable: This state of an element can be detected through the ExpectedCondition
elementToBeClickable()
where the expectation is to check if the element visible and enabled so that you can click it.-
Exmaple:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("css_of_element")));
-
You can find a detailed discussion in Selenium: Check for the presence of element