We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Master the art of querying element state, content, and styling without modifying them.
In this unit, you learned how to inspect WebElements to gather information about their state, content, and styling. Unlike interaction commands (click, sendKeys, etc.), inspection methods are read-only — they query the element without changing it.
Purpose: Check if an element is visible to the user
element.isDisplayed()
Returns true if visible, false if hidden by CSS
Purpose: Check if an element is interactive
button.isEnabled()
Returns false if element has disabled attribute
Purpose: Check if checkbox/radio is selected
checkbox.isSelected()
Only for checkboxes, radios, and <option> elements
Purpose: Get the HTML tag type
element.getTagName()
Returns lowercase tag name: "button", "a", "div"
Purpose: Get position and dimensions
element.getRect()
Returns Rectangle with x, y, width, height
Purpose: Get visible text content
element.getText()
For <input>, use getAttribute("value")
Purpose: Get computed CSS properties
element.getCssValue("color")
Colors return as rgba() format
Purpose: Get HTML attributes or DOM properties
getAttribute("value")
getDomAttribute("value")
getDomProperty("value")
Here's a complete example demonstrating all 7+ inspection methods in a real-world scenario:
123456789101112131415161718192021222324252627282930313233343536373839404142434445// Find the element
WebElement submitButton = driver.findElement(By.id("submit-btn"));
// 1. Check visibility
boolean isVisible = submitButton.isDisplayed();
System.out.println("Visible: " + isVisible);
// 2. Check if enabled
boolean isEnabled = submitButton.isEnabled();
System.out.println("Enabled: " + isEnabled);
// 3. Get tag name
String tagName = submitButton.getTagName();
System.out.println("Tag: " + tagName); // "button"
// 4. Get position and size
Rectangle rect = submitButton.getRect();
System.out.println("Position: (" + rect.getX() + ", " + rect.getY() + ")");
System.out.println("Size: " + rect.getWidth() + "x" + rect.getHeight());
// 5. Get text content
String buttonText = submitButton.getText();
System.out.println("Text: " + buttonText); // "Submit"
// 6. Get CSS styling
String bgColor = submitButton.getCssValue("background-color");
String fontSize = submitButton.getCssValue("font-size");
System.out.println("Background: " + bgColor); // "rgba(37, 99, 235, 1)"
System.out.println("Font Size: " + fontSize); // "16px"
// 7. Get attributes
String idAttr = submitButton.getAttribute("id");
String typeAttr = submitButton.getDomAttribute("type");
System.out.println("ID: " + idAttr);
System.out.println("Type: " + typeAttr);
// For checkboxes
WebElement checkbox = driver.findElement(By.id("terms"));
boolean isChecked = checkbox.isSelected();
System.out.println("Checkbox selected: " + isChecked);
// For input fields
WebElement input = driver.findElement(By.id("username"));
String inputValue = input.getAttribute("value");
System.out.println("Input value: " + inputValue);isDisplayed() and isEnabled() to ensure the element is ready for interaction.getText(), getAttribute(), and getCssValue() to verify content and state.getTagName() and getRect() to understand why locators aren't working as expected.getRect() to verify element positioning and sizing.Always verify element state before attempting interactions:
if (button.isDisplayed() &&
button.isEnabled()) {
button.click();
}findElement() finds elements even if hidden. Always use isDisplayed() to check visibility.
getText() for labels, buttons, headingsgetAttribute("value") for input fieldsisSelected() only works for checkboxes, radios, and <option> elements.getCssValue() returns rgba() format.getTagName() returns lowercase. Compare with "button", not "BUTTON".| Method | Returns | Use Case |
|---|---|---|
| isDisplayed() | boolean | Is element visible? |
| isEnabled() | boolean | Can user interact? |
| isSelected() | boolean | Is checkbox/radio selected? |
| getTagName() | String | Verify element type |
| getRect() | Rectangle | Get position & size |
| getText() | String | Get visible text |
| getCssValue(prop) | String | Get computed CSS |
| getAttribute(name) | String | Get attribute/property |