Se
Java ยท JUnit 5
Element handling with Selenium
15 copy-ready Selenium recipes, each linked to a live challenge page to run them against.
Conventions
// Helpers used below (plus static imports of ExpectedConditions.* and Assertions.*):
By tid(String id) { return By.cssSelector("[data-testid='" + id + "']"); }
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));1Type into inputs & read output
Practise on /elements/text-box โdriver.findElement(tid("full-name-input")).sendKeys("Ada Lovelace");
driver.findElement(tid("submit-text-box")).click();
assertEquals("Name: Ada Lovelace",
wait.until(visibilityOfElementLocated(tid("output-name"))).getText());2Checkboxes & select-all
Practise on /elements/check-box โdriver.findElement(tid("checkbox-select-all")).click();
assertTrue(driver.findElement(tid("checkbox-documents")).isSelected());3Native <select> dropdown
Practise on /widgets/selects-autocomplete โnew Select(driver.findElement(tid("native-select"))).selectByVisibleText("Mumbai");
assertEquals("Selected: Mumbai", driver.findElement(tid("native-select-result")).getText());4Autocomplete suggestions
Practise on /widgets/selects-autocomplete โdriver.findElement(tid("autocomplete-input")).sendKeys("Ben");
wait.until(elementToBeClickable(tid("autocomplete-option-bengaluru"))).click();
assertEquals("Picked: Bengaluru", driver.findElement(tid("autocomplete-result")).getText());5Find a table row by its text
Practise on /elements/web-tables โdriver.findElement(By.xpath(
"//tr[@data-testid='table-row'][contains(., 'Priya')]"
+ "//*[starts-with(@data-testid, 'delete-row-')]")).click();
assertEquals(2, driver.findElements(tid("table-row")).size());6Double-click & right-click
Practise on /elements/buttons โActions actions = new Actions(driver);
actions.doubleClick(driver.findElement(tid("double-click-me"))).perform();
actions.contextClick(driver.findElement(tid("right-click-me"))).perform();
assertTrue(driver.findElement(tid("right-click-result")).isDisplayed());7JS alert, confirm & prompt
Practise on /alerts โdriver.findElement(tid("prompt-button")).click();
Alert prompt = wait.until(alertIsPresent());
prompt.sendKeys("Ada");
prompt.accept();
assertEquals("You entered Ada", driver.findElement(tid("prompt-result")).getText());
driver.findElement(tid("confirm-button")).click();
wait.until(alertIsPresent()).dismiss();8Iframes & nested iframes
Practise on /frames โdriver.switchTo().frame(driver.findElement(tid("single-frame")));
driver.findElement(tid("single-frame-button")).click();
assertEquals("Clicked inside iframe", driver.findElement(tid("single-frame-result")).getText());
driver.switchTo().defaultContent();
driver.switchTo().frame(driver.findElement(tid("outer-frame")))
.switchTo().frame(driver.findElement(tid("innermost-frame")));9New tabs & windows
Practise on /windows โString original = driver.getWindowHandle();
driver.findElement(tid("new-tab-button")).click();
wait.until(numberOfWindowsToBe(2));
for (String h : driver.getWindowHandles())
if (!h.equals(original)) driver.switchTo().window(h);
assertTrue(wait.until(visibilityOfElementLocated(tid("new-tab-heading"))).isDisplayed());10HTML5 drag & drop
Practise on /interactions/droppable โ// Actions.dragAndDrop can't fire HTML5 drag events in Chrome โ dispatch them with JS.
((JavascriptExecutor) driver).executeScript(
"const [s, t] = arguments, dt = new DataTransfer();"
+ "s.dispatchEvent(new DragEvent('dragstart', {bubbles: true, dataTransfer: dt}));"
+ "t.dispatchEvent(new DragEvent('drop', {bubbles: true, dataTransfer: dt}));",
driver.findElement(tid("draggable-box")), driver.findElement(tid("accept-zone")));11File upload
Practise on /elements/upload-download โString path = new java.io.File("src/test/resources/sample.txt").getAbsolutePath();
driver.findElement(tid("file-input")).sendKeys(path);
assertTrue(driver.findElement(tid("uploaded-file-info")).getText().contains("sample.txt"));12Hover tooltips
Practise on /widgets/tooltips-menus โnew Actions(driver).moveToElement(driver.findElement(tid("tooltip-trigger"))).perform();
wait.until(visibilityOfElementLocated(tid("tooltip")));13Wait for dynamically loaded content
Practise on /dynamic/dynamic-loading โdriver.findElement(tid("loading-1-start")).click();
WebElement result = wait.until(visibilityOfElementLocated(tid("loading-1-result")));
assertTrue(result.getText().contains("Hello World!"));14Shadow DOM
Practise on /shadow-dom โSearchContext root = driver.findElement(By.cssSelector("shadow-counter")).getShadowRoot();
root.findElement(By.cssSelector("#inc")).click();
assertEquals("Count: 1", root.findElement(By.cssSelector("#count")).getText());15Keyboard keys
Practise on /interactions/key-presses โdriver.findElement(tid("key-input")).sendKeys(Keys.ENTER);
assertEquals("You entered: Enter", driver.findElement(tid("key-result")).getText());