About this lesson
Selenium Python find_elements with a duplicate id: count the matches and test the first one
Selenium Python AssignmentsBeginnerLocators
How it works
In Selenium Python the plural method find_elements returns an ordinary list of WebElement objects. When nothing matches, the list is empty and no exception is raised. Its singular twin, find_element, returns the first match in document order and raises NoSuchElementException when there is none. Pick the plural form whenever the question is "how many".
HTML says an id should be unique, but the browser does not enforce it. This page puts one id on two different input elements. find_element hands you the first and never mentions the second, so a test can pass while the markup is broken. A CSS search and an XPath search for the same id also report two, so the duplicate is real.
On the Python side, len() gives an int and is_displayed() is a method that returns a bool. Joining either one to a string with + raises TypeError: can only concatenate str (not "int") to str, or (not "bool") for the bool. Wrap the value in str() or use an f-string. Indexing the list with [0] picks the first match.
About the assignment
Open the Text Fields page, collect every element that carries the shared id, and report two facts: how many there are, and whether the first one is visible to a user.
What you will do
- Search with the plural lookup method and the id strategy, and keep the returned list in a variable.
- Take the length of the list and turn it into a string before joining it to the label.
- Call
is_displayed()on the first item. Remember the parentheses and the string conversion again. - Print the count line first and the visibility line second, with the labels from the task.
Common mistakes
Joining an int or bool to a string
A label plus
len(matches)stops withTypeErrorwith the messagecan only concatenate str (not "int") to str. Aboolraises the same error, namingboolin the message. Usestr()or an f-string.Indexing a list that came back empty
If you misspell the id,
find_elementsreturns[]and the failure moves to your[0]line asIndexError: list index out of range. The real cause is the locator.Calling len() on a single element
find_elementreturns oneWebElement, andlen()on it raisesTypeError: object of type 'WebElement' has no len(). Counting needs the plural method.
In a real test suite
Teams use the count as a cheap guard. A check such as assert len(elements) == 1 in a page object catches duplicated ids and repeated buttons before a click lands on the wrong element. In pytest that helper often lives in conftest.py or a base page class.
The next lesson covers the opposite failure, where nothing matches and find_element raises.
Related lessons
- Find Every Radio ButtonAnother
find_elementscall, used to collect a whole group of radio buttons into a list. - Fix a Locator That Does Not ExistShows what
find_elementdoes when the locator matches nothing at all. - Type into a Text FieldUses this same page and id, and relies on
find_elementchoosing the first duplicate.
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/TextFields")
# Count elements with id firstName and print whether the first is displayed
finally:
driver.quit()