Skip to content
CodeKruPro

Two Elements Share One ID

11/80

Two Elements Share One ID

An id is supposed to be unique. This page has two elements with id firstName. find_elements returns both. find_element returns only the first.

Task:

  1. Find every element with id firstName
  2. Print how many there are
  3. Print whether the first one is displayed

Print the lines below exactly.

Expected output:

Matches: 2
First displayed: True

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

  1. Search with the plural lookup method and the id strategy, and keep the returned list in a variable.
  2. Take the length of the list and turn it into a string before joining it to the label.
  3. Call is_displayed() on the first item. Remember the parentheses and the string conversion again.
  4. 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 with TypeError with the message can only concatenate str (not "int") to str. A bool raises the same error, naming bool in the message. Use str() or an f-string.

  • Indexing a list that came back empty

    If you misspell the id, find_elements returns [] and the failure moves to your [0] line as IndexError: list index out of range. The real cause is the locator.

  • Calling len() on a single element

    find_element returns one WebElement, and len() on it raises TypeError: 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.

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/TextFields")
    # Count elements with id firstName and print whether the first is displayed
finally:
    driver.quit()