Skip to main content

WebElement Methods

WebElement methods are used to interact with and validate UI elements once they are located. Mastery of these methods is essential for reliable Selenium automation.


What is WebElement?

WebElement represents a single element on a web page. All interactions like clicking, typing, and reading values are performed using WebElement APIs.

WebElement element = driver.findElement(By.id("username"));

Core Interaction Methods

click()

Clicks on buttons, links, checkboxes, radio buttons.

element.click();

Use after ensuring the element is clickable (waits).


sendKeys()

Types text into input fields.

element.sendKeys("admin");

Used for:

  • Text fields
  • Password fields
  • File upload (basic)

clear()

Clears existing text from input fields.

element.clear();

Read / Fetch Methods

getText()

Returns visible text of an element.

String text = element.getText();

Used for UI validations.


getAttribute()

Fetches attribute values.

String value = element.getAttribute("value");

Common attributes:

  • value
  • href
  • src
  • class

State Validation Methods

isDisplayed()

Checks if element is visible.

element.isDisplayed();

isEnabled()

Checks if element is enabled.

element.isEnabled();

isSelected()

Checks selection state (checkbox/radio).

element.isSelected();

Common Exceptions to Expect

  • ElementNotInteractableException
  • StaleElementReferenceException
  • NoSuchElementException

(Handled via waits and retries — covered later.)


Best Practices ✅

  • Always wait before interaction
  • Re-locate elements after navigation
  • Use meaningful assertions
  • Avoid chaining actions blindly

Common Mistakes ❌

  • Clicking without waits
  • Using getText() for hidden values
  • Ignoring stale elements
  • Assuming visibility = clickability

Key Takeaways

  • WebElement is the core interaction unit
  • Separate locate and interact logic
  • Validate element state before action
  • Clean usage improves stability