Skip to content
CodeKruPro

Find Multiple Elements with findElements()

10/70

Video walkthrough

Watch on YouTube

Find Multiple Elements with findElements()

While findElement() returns a single element, findElements() returns a List<WebElement> of all matching elements.

You can pass findElements() any locator you already know, such as By.tagName(), By.name() or By.className().

On the Radio Buttons page, there are multiple radio button inputs, and every <input> on the page is a radio button.

Task:

  1. Navigate to https://testkru.com/Elements/RadioButtons
  2. Find ALL radio button inputs with findElements() and By.tagName(), using the tag name input
  3. Print the total count
  4. Print the ID of the first radio button

Expected output:

Total radio buttons: 15
First radio ID: firstSelect1

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

  1. Call driver.findElements() with By.tagName() and the tag that every radio button on the page uses.
  2. Print the number of elements in the list after the label Total radio buttons:.
  3. Take the first element with get(0), read its id attribute and print it after the label First 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 with isDisplayed() when you only want visible ones.

  • Indexing an empty list

    If the selector is wrong, the list is empty and get(0) throws IndexOutOfBoundsException, 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.

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.

Main.java
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();
    }
}