You can find elements. Now let's do things with them. The four actions you'll use in 90% of your tests: sendKeys() to type, click() to click, clear() to empty a field, getText() to read.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class BankingLoginAutomation {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://www.testerrank.com/banking/login");
// Type into a text field
WebElement userIdField = driver.findElement(
By.cssSelector("[data-testid='userId']"));
userIdField.clear(); // Always clear first
userIdField.sendKeys("rahul@netbank.com");
// Type password
WebElement passwordField = driver.findElement(
By.cssSelector("[data-testid='password']"));
passwordField.clear();
passwordField.sendKeys("Bank@123");
// Click a button
WebElement loginBtn = driver.findElement(
By.cssSelector("[data-testid='loginBtn']"));
loginBtn.click();
// Read text from an element
WebElement welcome = driver.findElement(
By.cssSelector("[data-testid='welcomeUser']"));
System.out.println("Welcome: " + welcome.getText());
// Read an attribute value
String placeholder = userIdField.getAttribute("placeholder");
System.out.println("Placeholder: " + placeholder);
// Check if an element is visible
boolean isVisible = welcome.isDisplayed();
System.out.println("Welcome visible: " + isVisible);
driver.quit();
}
}| Method | Returns | What It Does |
|---|---|---|
| sendKeys("text") | void | Type text into an input field |
| click() | void | Click the element |
| clear() | void | Clear text from an input field |
| getText() | String | Get visible text content |
| getAttribute("attr") | String | Get an HTML attribute value |
| isDisplayed() | boolean | Is the element visible on screen? |
| isEnabled() | boolean | Is the element clickable (not disabled)? |
| isSelected() | boolean | Is the checkbox/radio checked? |
| getTagName() | String | Get the HTML tag (input, button, div...) |
| getCssValue("prop") | String | Get a CSS property value |
HTML <select> dropdowns need the Select class. Regular click() won't work on them.
import org.openqa.selenium.support.ui.Select;
WebElement dropdownElement = driver.findElement(By.id("accountType"));
Select dropdown = new Select(dropdownElement);
// Three ways to select:
dropdown.selectByVisibleText("Savings Account"); // What user sees
dropdown.selectByValue("savings"); // value attribute
dropdown.selectByIndex(0); // Position (0-based)
// Read the selected option
String selected = dropdown.getFirstSelectedOption().getText();
// Get all options
List<WebElement> allOptions = dropdown.getOptions();
for (WebElement opt : allOptions) {
System.out.println(opt.getText());
}The Select class ONLY works with native HTML <select> elements. Many modern apps use custom dropdowns built with div/ul/li. For those, you need to click the dropdown to open it, then click the option you want. This catches a lot of freshers in interviews.
// Checkbox — check if selected, then toggle
WebElement rememberMe = driver.findElement(
By.cssSelector("[data-testid='rememberMe']"));
if (!rememberMe.isSelected()) {
rememberMe.click(); // Check it
}
// Radio button — same approach
WebElement savingsRadio = driver.findElement(
By.cssSelector("input[type='radio'][value='savings']"));
if (!savingsRadio.isSelected()) {
savingsRadio.click();
}Q: How do you handle dropdowns in Selenium?
A: For native HTML <select> dropdowns, I use the Select class from org.openqa.selenium.support.ui. I wrap the <select> WebElement in a Select object, then use selectByVisibleText(), selectByValue(), or selectByIndex(). For custom dropdowns built with div/ul/li, the Select class doesn't work — I click the dropdown to open it, wait for the options to appear, then click the desired option.
Key Point: sendKeys() types, click() clicks, clear() clears, getText() reads. For dropdowns, use the Select class. For checkboxes, check isSelected() before clicking.