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
- Search for all inputs whose type is radio, using the plural search method, and store the list.
- Take the length of the list and turn it into text.
- Print the label
Total radio buttons:, a space, and the count. - 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
TypeErrorwith the messagecan only concatenate str (not "int") to str.Using find_element and then len
The singular method returns one element, and
len()on it raisesTypeError: object of type 'WebElement' has no len(). Use the plural.Indexing an empty list
A selector with a typo returns
[], and[0]raisesIndexError: 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.
Related lessons
- Two Elements Share One IDLooks at a case where more than one element matches an id.
- Choose One Radio in a GroupPicks one radio from a group once you can see them all.
- List Every Dropdown OptionLists every option of a dropdown, another loop over many items.
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.
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()