Selenium - User Interactions
Selenium - User Interactions
Section titled “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.
Basic Element Interactions
Section titled “Basic Element Interactions”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();Interacting with Select Dropdowns
Section titled “Interacting with Select Dropdowns”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 textcountrySelect.selectByVisibleText("United States");
// Select by the value attribute of the <option> tagcountrySelect.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()).
Advanced User Actions (Actions Class)
Section titled “Advanced User Actions (Actions Class)”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);Mouse Hover (Move to Element)
Section titled “Mouse Hover (Move to Element)”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 appearWebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));wait.until(ExpectedConditions.visibilityOf(subMenu));subMenu.click(); // Click the submenu itemDrag and Drop
Section titled “Drag and Drop”WebElement sourceElement = driver.findElement(By.id("draggable"));WebElement targetElement = driver.findElement(By.id("droppable"));
actions.dragAndDrop(sourceElement, targetElement).perform();Right-Click (Context Click)
Section titled “Right-Click (Context Click)”WebElement elementToRightClick = driver.findElement(By.id("contextMenuArea"));
actions.contextClick(elementToRightClick).perform();// Add steps to interact with the context menu that appearsKeyboard Actions (e.g., SHIFT + Click)
Section titled “Keyboard Actions (e.g., SHIFT + Click)”WebElement item1 = driver.findElement(By.id("item1"));WebElement item3 = driver.findElement(By.id("item3"));
// Select multiple items using SHIFT keyactions.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.
Synchronization
Section titled “Synchronization”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.