Skip to content

Selenium - User Interactions

Selenium WebDriver allows you to simulate a wide range of user interactions with web elements. These range from basic actions like clicking and typing to more complex gestures like drag-and-drop or keyboard shortcuts.

Most common interactions are performed directly on WebElement objects:

  • Typing Text: Use sendKeys() for input fields and text areas.
  • Clicking: Use click() for buttons, links, radio buttons, checkboxes, etc.
  • Clearing Text: Use clear() to remove existing text from an input field before typing.
  • Submitting Forms: Use submit() on any element within a form, or click the submit button directly. submit() works even if the element isn’t a button.
WebElement usernameInput = driver.findElement(By.id("username"));
WebElement passwordInput = driver.findElement(By.id("password"));
WebElement loginButton = driver.findElement(By.id("loginBtn"));
usernameInput.clear();
usernameInput.sendKeys("testuser");
passwordInput.clear();
passwordInput.sendKeys("securePassword");
loginButton.click(); // Or passwordInput.submit();

For standard HTML <select> elements, Selenium provides a dedicated Select class for easier interaction:

import org.openqa.selenium.support.ui.Select;
WebElement countryDropdownElement = driver.findElement(By.id("countrySelect"));
Select countrySelect = new Select(countryDropdownElement);
// Select by visible text
countrySelect.selectByVisibleText("United States");
// Select by the value attribute of the <option> tag
countrySelect.selectByValue("US");
// Select by the index (0-based)
countrySelect.selectByIndex(1);

The Select class also provides methods for deselecting options (deselectBy...(), deselectAll()) and retrieving selected options (getFirstSelectedOption(), getAllSelectedOptions()).

For more complex interactions like mouse movements, drag-and-drop, keyboard modifiers (Shift, Ctrl, Alt), and context clicks (right-clicks), WebDriver provides the Actions class. You build a sequence of actions and then call perform() to execute them.

import org.openqa.selenium.interactions.Actions;
Actions actions = new Actions(driver);
WebElement mainMenu = driver.findElement(By.id("mainMenu"));
WebElement subMenu = driver.findElement(By.id("subMenuLink"));
actions.moveToElement(mainMenu).perform(); // Hover over main menu
// Add a short wait here if needed for submenu to appear
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.visibilityOf(subMenu));
subMenu.click(); // Click the submenu item
WebElement sourceElement = driver.findElement(By.id("draggable"));
WebElement targetElement = driver.findElement(By.id("droppable"));
actions.dragAndDrop(sourceElement, targetElement).perform();
WebElement elementToRightClick = driver.findElement(By.id("contextMenuArea"));
actions.contextClick(elementToRightClick).perform();
// Add steps to interact with the context menu that appears
WebElement item1 = driver.findElement(By.id("item1"));
WebElement item3 = driver.findElement(By.id("item3"));
// Select multiple items using SHIFT key
actions.click(item1)
.keyDown(Keys.SHIFT) // Press Shift
.click(item3) // Click the second item
.keyUp(Keys.SHIFT) // Release Shift
.perform();

The Actions class offers many other methods like doubleClick(), clickAndHold(), release(), sendKeys(Keys.chord(Keys.CONTROL, "a")) etc. Refer to the Selenium documentation for a full list.

Modern web applications often load content dynamically. Directly interacting with an element that hasn’t loaded yet will cause your script to fail. Selenium provides Waits to handle this synchronization. Using appropriate waits is crucial for stable tests. This is covered in detail in the ‘Selenium - Waits’ chapter.