We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Learn the mental model of interactions and the 5 basic commands: click, sendKeys, clear, submit, and Select.
Finding an element is only half the battle. To automate a real user flow, you need to interact with elements—typing, clicking, and selecting.
Locator (like By.id) to find the element, and then you call methods on the resulting WebElement to interact with it.Simulates a mouse click. Used for buttons, links, checkboxes, and radio buttons.
Types text into an input field. Can also send special keys like Keys.ENTER.
Erases text from an input. Crucial before typing to ensure no old data remains.
Submits the form an element belongs to. (Prefer clicking the submit button usually).
Helper for <select> dropdowns. Provides selectByVisibleText, etc.
When you call an interaction method, WebDriver tries to act like a real user. It checks if the element is:
If any of these fail, you'll get an exception (e.g., ElementNotInteractableException).
Think in two lines: one for finding, one for doing.
By usernameLocator = By.id("username-input");
WebElement usernameInput = driver.findElement(usernameLocator);
usernameInput.sendKeys("testuser123");Always clear before typing in form fields to avoid flakiness.
usernameInput.clear();
usernameInput.sendKeys("new-value");Wrap standard <select> elements with the Select class.
WebElement roleDropdown = driver.findElement(By.id("role-select"));
Select roleSelect = new Select(roleDropdown);
roleSelect.selectByVisibleText("Tester");1234567891011121314WebElement usernameInput = driver.findElement(By.id("username-input"));
usernameInput.clear();
usernameInput.sendKeys("testuser123");
WebElement roleDropdown = driver.findElement(By.id("role-select"));
Select roleSelect = new Select(roleDropdown);
roleSelect.selectByVisibleText("Tester");
WebElement submitButton = driver.findElement(By.id("submit-btn"));
submitButton.click();
// Verification
String currentUsername = usernameInput.getAttribute("value");
String selectedRole = roleSelect.getFirstSelectedOption().getText();usernameInput, not elem1.fillLoginForm().click() on a By locator.ElementNotInteractableException.