Clicks, Typing, Dropdowns, Alerts, Frames and Windows
Once you can find an element, you need to do something with it. These are the actions you will use in almost every project.
Text fields and buttons
WebElement email = driver.findElement(By.id("email"));
email.clear(); // remove old text
email.sendKeys("demo@testqualityhub.com");
email.sendKeys(Keys.ENTER); // press a key
driver.findElement(By.id("submit")).click();
Reading from the page
String text = element.getText(); // visible text
String value = element.getAttribute("value"); // what is typed in an input
boolean shown = element.isDisplayed();
boolean enabled = element.isEnabled();
boolean ticked = element.isSelected(); // checkbox or radio
Checkboxes and radio buttons
WebElement remember = driver.findElement(By.id("remember"));
if (!remember.isSelected()) {
remember.click();
}
Always check the state first. Clicking a ticked box unticks it.
Dropdowns (select elements)
Select country = new Select(driver.findElement(By.id("country")));
country.selectByVisibleText("Canada");
country.selectByValue("ca");
String chosen = country.getFirstSelectedOption().getText();
Select only works on a real <select> element. Custom dropdowns built from divs are clicked like any other element: click to open, then click the option.
JavaScript alerts
Alert alert = driver.switchTo().alert();
String message = alert.getText();
alert.accept(); // OK
// alert.dismiss(); // Cancel
Iframes
An iframe is a page inside a page. Selenium cannot see inside it until you switch.
driver.switchTo().frame("payment-frame"); // by id/name or WebElement
driver.findElement(By.id("card-number")).sendKeys("4242424242424242");
driver.switchTo().defaultContent(); // back to the main page
New tabs and windows
String original = driver.getWindowHandle();
driver.findElement(By.linkText("Open in new tab")).click();
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(original)) {
driver.switchTo().window(handle);
}
}
// ... test the new tab ...
driver.close();
driver.switchTo().window(original);
Mouse and keyboard actions
Actions actions = new Actions(driver);
actions.moveToElement(menu).perform(); // hover
actions.doubleClick(cell).perform();
actions.contextClick(row).perform(); // right click
actions.dragAndDrop(source, target).perform();
Screenshots
File shot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
Files.copy(shot.toPath(), Path.of("target/login.png"), StandardCopyOption.REPLACE_EXISTING);
Practice: automate the buggy cart: add items, change quantities and assert the total. You will find at least one bug while doing it.
All 9 lessons in Selenium with Java
- What Is Selenium WebDriver?
- Set Up Java, Maven and Selenium
- Write Your First Selenium Test
- Selenium Locators: ID, CSS, XPath and More
- Clicks, Typing, Dropdowns, Alerts, Frames and Windows
- Waits in Selenium: Implicit, Explicit and Fluent
- TestNG for Selenium: Annotations, Assertions and Data
- Page Object Model in Selenium Java
- Selenium Framework Structure, Reports and CI