About this lesson
findElements in Selenium Java: get a List of matching elements and count them
Selenium Java AssignmentsBeginnerLocators
How it works
driver.findElements(locator) returns a List<WebElement> with every element the locator matches, in document order. It never throws for a missing element; when nothing matches you get an empty list. That makes it the safe way to ask whether something exists and how many there are.
The list is a snapshot taken at the moment of the call. Elements added to the page afterwards do not appear in it, and elements removed later become stale inside it.
Because the list keeps document order, get(0) is the first radio in the document, the first single-select option, and size() is an ordinary Java List count you can assert on.
About the assignment
The Radio Buttons page groups its radio inputs into rows: single-select, multi-select, disabled, hidden and pre-selected. It has no other form fields, so every input element on it is a radio. You will collect all of them with the tag name locator from lesson 7, count them, and read the id of the first.
What you will do
- Call
driver.findElements()withBy.tagName()and the tag that every radio button on the page uses. - Print the number of elements in the list after the label
Total radio buttons:. - Take the first element with
get(0), read itsidattribute and print it after the labelFirst radio ID:.
Common mistakes
Expecting hidden elements to be left out
The count includes every match in the DOM. That includes the hidden row, whose radios have
visibility: hidden. Filter withisDisplayed()when you only want visible ones.Indexing an empty list
If the selector is wrong, the list is empty and
get(0)throwsIndexOutOfBoundsException, an error that hides the real cause. Assert that the list is not empty first.Assuming a tag means a radio
The tag name works here only because this page holds nothing but radios. On the Text Fields page the same locator returns text boxes, so check
getAttribute("type")before trusting a tag-based count elsewhere.Using findElements() to wait
With no implicit wait set, it returns immediately with whatever is there. An empty list may only mean the page has not finished rendering; waits are covered from lesson 35.
In a real test suite
Real suites use findElements() for search results, table rows and validation messages, and to assert absence: an empty list for an error banner is a clean way to check that no error appeared.
The locators so far each read one attribute. Lesson 11 starts the CSS selector lessons, which let one locator combine tag, id and class.
Related lessons
- Find Element by Partial Link TextPartial link text often matches several links; this is how you see them all.
- Count Table Rows and ColumnsCount table rows and columns with the same
findElements()andsize()pair. - Get All Dropdown Options with getOptions()A dropdown's
getOptions()returns the same kind of list.
To try one of these calls on any page without a task, open the online Selenium compiler for Java.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
WebDriver driver = new ChromeDriver();
driver.get("https://testkru.com/Elements/RadioButtons");
// TODO: Find all radio buttons using findElements and By.tagName("input")
// TODO: Print the total count
// TODO: Print the ID of the first radio button
driver.quit();
}
}