WebElement Actions
Once findElement() hands you a WebElement, the real work starts: typing into fields, clicking buttons, reading back what's on the page, and checking an element's state before you touch it. This lesson walks through every action you'll use on nearly every test, building up to a complete "fill out and submit the Add Recipe form" sequence on RecipeBox.
click()
Simulates a mouse click. Works on buttons, links, checkboxes, radio buttons — anything clickable.
driver.findElement(By.id("loginBtn")).click();
sendKeys() and clear()
sendKeys() types text into an input field character by character, exactly like a real user typing. It appends to whatever is already there, so if a field might have leftover text (e.g. a placeholder value or a previous test run's leftover input), call clear() first.
WebElement titleField = driver.findElement(By.id("recipeTitle"));
titleField.clear();
titleField.sendKeys("Paneer Butter Masala");
getText() and getAttribute()
getText() returns the visible, rendered text of an element — great for headings, labels, and error messages. getAttribute("attributeName") reads any HTML attribute, including ones with no visible text at all, such as an input's current value.
// Verify the recipe list page loaded the recipe we just added
WebElement heading = driver.findElement(By.xpath("//h3[contains(text(),'Paneer')]"));
System.out.println("Recipe title text: " + heading.getText());
// Read back what's actually typed into the search box
WebElement searchInput = driver.findElement(By.id("searchInput"));
String typedValue = searchInput.getAttribute("value");
System.out.println("Search box currently holds: " + typedValue);
State Checks: isDisplayed(), isEnabled(), isSelected()
Before clicking or typing, it's often worth confirming the element is actually in a usable state — especially right after a page transition, where an element can exist in the DOM slightly before it's interactable.
| Method | Returns true when... |
|---|---|
| isDisplayed() | The element is visible on the rendered page (not display:none, not visibility:hidden, not zero-size). |
| isEnabled() | The element does not have the disabled attribute — e.g. the Save button isn't greyed out. |
| isSelected() | A checkbox/radio button/option is currently checked or selected. |
WebElement saveBtn = driver.findElement(By.id("saveRecipeBtn"));
if (saveBtn.isDisplayed() && saveBtn.isEnabled()) {
saveBtn.click();
} else {
System.out.println("Save button is not ready to be clicked yet");
}
Putting It Together: Filling the Add Recipe Form
Here's a complete sequence covering everything above, from opening the form to confirming the save succeeded.
driver.findElement(By.id("addRecipeBtn")).click();
WebElement titleField = driver.findElement(By.id("recipeTitle"));
titleField.clear();
titleField.sendKeys("Paneer Butter Masala");
WebElement saveBtn = driver.findElement(By.id("saveRecipeBtn"));
if (saveBtn.isEnabled()) {
saveBtn.click();
}
// Confirm the new recipe now shows up in the list
WebElement newRecipe = driver.findElement(By.xpath("//h3[contains(text(),'Paneer')]"));
System.out.println("Added: " + newRecipe.getText());