Skip to content
CodeKruPro

Find Every Radio Button

10/80

Find Every Radio Button

find_elements returns every match. len is the count. The first item is index 0.

Task:

  1. Open Radio Buttons
  2. Find every input whose type is radio
  3. Print the count and the first id

Print the lines below exactly.

Expected output:

Total radio buttons: 15
First radio ID: firstSelect1

About this lesson

Selenium Python find_elements: get a list of radio buttons, count them with len

Selenium Python AssignmentsBeginnerLocators

How it works

So far every search returned a single element. driver.find_elements() has an almost identical name, but it returns a Python list of every element that matches, in the order they appear on the page. Everything you know about lists applies to it: len() gives the count, an index such as [0] gives the first item, [-1] the last, a slice takes a few, and a loop visits each one.

The empty case differs most. If nothing matches, find_elements does not raise NoSuchElementException. It returns an empty list. Indexing into that list then fails with IndexError: list index out of range, so a missing element shows up one line later than you might expect.

Two Python points matter when you build the report line. len() returns an int, and joining an int to a label with + raises TypeError with the message can only concatenate str (not "int") to str, so convert it with str(). And a single element is not a list: asking len() of the result of find_element fails with TypeError: object of type 'WebElement' has no len().

About the assignment

The Radio Buttons page holds several groups of radios. You collect every radio input in one search, report how many there are, and report the id of the first one in page order.

What you will do

  1. Search for all inputs whose type is radio, using the plural search method, and store the list.
  2. Take the length of the list and turn it into text.
  3. Print the label Total radio buttons:, a space, and the count.
  4. Take the item at position zero, read its id attribute, and print it after the label First radio ID:.

Common mistakes

  • Joining the count without str()

    The count is an int. Adding it to a string raises TypeError with the message can only concatenate str (not "int") to str.

  • Using find_element and then len

    The singular method returns one element, and len() on it raises TypeError: object of type 'WebElement' has no len(). Use the plural.

  • Indexing an empty list

    A selector with a typo returns [], and [0] raises IndexError: list index out of range. When you see it, check the selector first.

In a real test suite

Suites often wrap this call in a helper that waits until the list is not empty, then returns it, so tests do not race the page.

After these basic strategies, the next lesson looks at a page where two elements share one id, and what Selenium does about it.

To try one of these calls on any page without a task, open the Online Selenium Python Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

main.py
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
try:
    driver.get("https://testkru.com/Elements/RadioButtons")
    # Count input[type=radio] and print the first id
finally:
    driver.quit()