We check if elements are visible, clickable, or selected to make sure our tests interact with the right parts of a webpage.
Checking state (isDisplayed, isEnabled, isSelected) in Selenium Java
boolean isDisplayed = element.isDisplayed(); boolean isEnabled = element.isEnabled(); boolean isSelected = element.isSelected();
isDisplayed() returns true if the element is visible on the page.
isEnabled() returns true if the element can be interacted with (not disabled).
isSelected() returns true if the element is selected (applicable for checkboxes, radio buttons, and options).
WebElement button = driver.findElement(By.id("submitBtn"));
boolean visible = button.isDisplayed();WebElement checkbox = driver.findElement(By.name("agreeTerms"));
boolean selected = checkbox.isSelected();WebElement inputField = driver.findElement(By.cssSelector("input[type='text']"));
boolean enabled = inputField.isEnabled();This program opens a webpage, finds the submit button and terms checkbox, then prints if the button is visible and enabled, and if the checkbox is selected.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class CheckElementState { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com/form"); WebElement submitButton = driver.findElement(By.id("submitBtn")); WebElement termsCheckbox = driver.findElement(By.id("terms")); boolean isSubmitVisible = submitButton.isDisplayed(); boolean isSubmitEnabled = submitButton.isEnabled(); boolean isTermsSelected = termsCheckbox.isSelected(); System.out.println("Submit button visible: " + isSubmitVisible); System.out.println("Submit button enabled: " + isSubmitEnabled); System.out.println("Terms checkbox selected: " + isTermsSelected); } finally { driver.quit(); } } }
Always check if an element is displayed before interacting to avoid errors.
isSelected() works mainly for checkboxes, radio buttons, and options in dropdowns.
Elements not enabled cannot be clicked or typed into.
Use isDisplayed() to check visibility.
Use isEnabled() to check if element can be used.
Use isSelected() to check selection state of checkboxes or radios.
Practice
Solution
Step 1: Understand the purpose of
This method checks if an element is visible on the page, meaning it is rendered and not hidden.isDisplayed()Step 2: Compare with other methods
isEnabled()checks if the element can be interacted with,isSelected()checks selection state, andgetText()returns text content, so they don't check visibility.Final Answer:
isDisplayed() -> Option DQuick Check:
Visibility check = isDisplayed() [OK]
- Confusing isDisplayed() with isEnabled()
- Using isSelected() for visibility
- Thinking getText() checks visibility
Solution
Step 1: Recall the method to check selection state
The correct method to check if a checkbox or radio button is selected isisSelected().Step 2: Verify syntax correctness
checkbox.isSelected();is valid Java syntax.getSelected()does not exist, and the others check different states.Final Answer:
checkbox.isSelected(); -> Option AQuick Check:
Check selection = isSelected() [OK]
- Using getSelected() which is invalid
- Confusing isDisplayed() with isSelected()
- Using isEnabled() to check selection
WebElement button = driver.findElement(By.id("submitBtn"));
System.out.println(button.isDisplayed());
System.out.println(button.isEnabled());Solution
Step 1: Understand isDisplayed() output
The button is visible, soisDisplayed()returnstrue.Step 2: Understand isEnabled() output
The button is disabled, soisEnabled()returnsfalse.Final Answer:
true false -> Option BQuick Check:
Visible = true, Enabled = false [OK]
- Assuming disabled means not displayed
- Mixing up isDisplayed() and isEnabled() results
- Expecting both to be true always
WebElement radio = driver.findElement(By.name("gender"));
if(radio.isEnabled()) {
System.out.println("Selected");
} else {
System.out.println("Not Selected");
}Solution
Step 1: Understand the purpose of isEnabled() vs isSelected()
isEnabled()checks if the element can be interacted with, not if it is selected.Step 2: Identify correct method to check selection
To check if a radio button is selected,isSelected()must be used.Final Answer:
Should use isSelected() instead of isEnabled() -> Option AQuick Check:
Selection check = isSelected() [OK]
- Using isEnabled() to check selection
- Assuming enabled means selected
- Ignoring method purpose differences
Solution
Step 1: Understand the required conditions
The checkbox must be visible (isDisplayed()), enabled (isEnabled()), and not selected (!isSelected()).Step 2: Analyze each option
if(checkbox.isDisplayed() && checkbox.isEnabled() && !checkbox.isSelected()) { checkbox.click(); } uses AND (&&) to combine all correct conditions and negatesisSelected()to ensure it is not selected before clicking.Final Answer:
if(checkbox.isDisplayed() && checkbox.isEnabled() && !checkbox.isSelected()) { checkbox.click(); } -> Option CQuick Check:
Use AND for all true and NOT for selection [OK]
- Using OR (||) instead of AND (&&)
- Not negating isSelected() when needed
- Clicking when checkbox is disabled or hidden
Ready to prove it?
Pass the test and this pattern moves unknown
