We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
How Selenium turns locators into single elements and lists.
In Selenium, a locator (a By object) is just a definition of where an element should be. To actually talk to the browser and interact with elements, you use:
WebElement.NoSuchElementException.List<WebElement>.null..isEmpty().Use your Selenium script to interact with the elements below. Click the banner, click menu items, or type in the input fields to verify your locators are working correctly.
Single Element: id="single-banner"
Multiple Elements: class="menu-item"
Practice typing with sendKeys()
How to practice:
findElement(By.id("single-banner")).click() to click the bannerfindElements(By.cssSelector(".menu-item")) to get all menu items, then loop and click eachfindElement(By.id("search-box")).sendKeys("text") to type in the search box1234567891011121314151617181920By bannerLocator = By.id("single-banner");
By menuItemsLocator = By.cssSelector(".menu-item");
WebElement banner = driver.findElement(bannerLocator);
System.out.println("Clicking banner: " + banner.getText());
banner.click();
List<WebElement> menuItems = driver.findElements(menuItemsLocator);
System.out.println("Found " + menuItems.size() + " menu items.");
for (WebElement item : menuItems) {
item.click();
}
List<WebElement> missing = driver.findElements(By.cssSelector(".menu-item-unknown"));
if (missing.isEmpty()) {
System.out.println("Element correctly missing (no exception thrown).");
}
driver.quit();By bannerLocator = By.id("single-banner")Creates a locator for the single banner using its unique id. Declaring it in a variable makes it reusable and easier to maintain.
By menuItemsLocator = By.cssSelector(".menu-item")Creates a locator for all navigation menu items using their shared CSS class .menu-item.
WebElement banner = driver.findElement(bannerLocator)Uses findElement with bannerLocator to get a single WebElement. If the banner is missing, an exception is thrown.
banner.click()Clicks the banner once to demonstrate single element interaction.
List<WebElement> menuItems = driver.findElements(menuItemsLocator)Uses findElements to get a list of all matching navigation items. If none are found, the list is just empty.
System.out.println("Found " + menuItems.size() + " menu items.")Logs how many menu items were found, useful for debugging and verification.
for (WebElement item : menuItems) {
item.click();
}Loops over each WebElement in the list and clicks it, demonstrating iteration over multiple elements.
List<WebElement> missing = driver.findElements(By.cssSelector(".menu-item-unknown"))Uses a locator that matches no elements. findElements returns an empty list, not an exception.
if (missing.isEmpty()) { ... }Checks if the list is empty. This is a safe existence check without try–catch.
driver.quit()Closes the browser and ends the WebDriver session, cleaning up resources.
Declare locators as By variables at the top of your test or page object. It keeps your code cleaner and makes locators easy to update.
Use findElement when you expect exactly one element and want a failure if it's missing.
Use findElements when you expect multiple elements, or just want to check if anything exists without throwing an exception (by checking list.isEmpty()).