18. WebElement Operations, State Verification & Text Retrieval

Core WebElement Methods

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");

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.
πŸ’‘Common doubts
Did you like the problem?
Lesson 18 of 44
Java
Loading code editor…
Next