Common Actions
The most frequently used WebDriver interactions.
Click, type, clear
java
WebElement input = driver.findElement(By.id("username"));
input.clear();
input.sendKeys("[email protected]");
driver.findElement(By.id("login-btn")).click();Dropdowns (Select)
java
import org.openqa.selenium.support.ui.Select;
Select dropdown = new Select(driver.findElement(By.id("country")));
dropdown.selectByVisibleText("United States");
dropdown.selectByValue("US");
dropdown.selectByIndex(2);
// Get selected option
String selected = dropdown.getFirstSelectedOption().getText();Hover, drag-and-drop (Actions API)
java
import org.openqa.selenium.interactions.Actions;
Actions actions = new Actions(driver);
// Hover
actions.moveToElement(driver.findElement(By.id("menu"))).perform();
// Drag and drop
WebElement source = driver.findElement(By.id("drag"));
WebElement target = driver.findElement(By.id("drop"));
actions.dragAndDrop(source, target).perform();
// Click and hold
actions.clickAndHold(source).moveToElement(target).release().perform();JavaScript executor
java
JavascriptExecutor js = (JavascriptExecutor) driver;
// Scroll element into view
js.executeScript("arguments[0].scrollIntoView(true);", element);
// Click via JS (useful when element is covered)
js.executeScript("arguments[0].click();", element);
// Get value
String val = (String) js.executeScript("return document.title;");