Skip to content
CodeKruPro

Find every matching element and count them

Automation taskEasyAsked in: Accenture, Capgemini, Infosys, TCS, Wipro

Asked as: "How many radio buttons are on this page?" or "What does findElements return?" The multi-element lookup returns a list you can count and loop over. When nothing matches, the list is empty and nothing is thrown.

Find Multiple Elements with findElements()

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