Selenium + Java · Lesson 18 of 44

0/44 done

Module 18: WebElement

18. WebElement Operations, State Verification & Text Retrieval

16 minlesson

Your task

Implement enterTextIfEnabled:

  1. Accept WebElement element and String textToEnter.
  2. If element != null and element.isDisplayed() and element.isEnabled(), clear it and sendKeys(textToEnter).
  3. Return true if typed, otherwise false.

Write it in the editor, then press Check answer (Ctrl + Enter).

Core WebElement Methods

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

// Input operations:
usernameField.clear();
usernameField.sendKeys("tester_admin");

// State checks:
boolean visible = usernameField.isDisplayed();
boolean enabled = usernameField.isEnabled();

// Retrieving information:
String currentText = usernameField.getAttribute("value");
String placeholder = usernameField.getAttribute("placeholder");

Key Takeaways & Walkthrough Summary

WebElement represents an HTML element on the page. In this lesson, you will master click, sendKeys, clear, getText, and checking states like isDisplayed and isEnabled before performing user actions. Let's master WebElement!

Part of the Selenium + Java course (Selenium with Java). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Why does element.getText() return an empty string for an input field?

Text typed into <input> elements lives in the 'value' attribute, not the inner text. Call element.getAttribute('value') instead.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Java
Skip