Selenium + Java · Lesson 18 of 44
Module 18: WebElement
Your task
Implement enterTextIfEnabled:
Write it in the editor, then press Check answer (Ctrl + Enter).
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");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.
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.
___