Finding multiple elements helps you check many parts of a webpage at once. It is useful when you want to test lists, tables, or groups of similar items.
You want to check all buttons on a page to see if they are clickable.
You need to verify all links in a menu are present and correct.
You want to count how many items appear in a search result.
You want to test if all images in a gallery load properly.
Syntax
Selenium Python
elements = driver.find_elements(By.LOCATOR_TYPE, "locator_value")
find_elements returns a list of matching elements, even if empty.
Use By to specify how to find elements, like by ID, class, or tag.
This script opens a small HTML page with a list of fruits. It finds all list items with the class 'fruit' and prints their names one by one.
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
# Setup driver with headless option (make sure chromedriver is in PATH)
options = Options()
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
# Open a simple test page
html = '''
<html>
<body>
<ul>
<li class="fruit">Apple</li>
<li class="fruit">Banana</li>
<li class="fruit">Cherry</li>
</ul>
</body>
</html>
'''# Load the HTML content
driver.get("data:text/html;charset=utf-8," + html)
# Find all list items with class 'fruit'
fruits = driver.find_elements(By.CLASS_NAME, "fruit")
# Print the text of each fruitfor fruit in fruits:
print(fruit.text)
# Close the driver
driver.quit()
OutputSuccess
Important Notes
If no elements match, find_elements returns an empty list, not an error.
Always close the driver after tests to free resources.
Summary
Use find_elements to get many elements at once.
The result is a list you can loop through.
It helps test groups of similar page parts easily.
Practice
(1/5)
1. What does the find_elements method in Selenium Python return when used to locate multiple elements?
easy
A. A list of WebElement objects matching the locator
B. A single WebElement object
C. A boolean indicating if elements exist
D. An integer count of elements found
Solution
Step 1: Understand the purpose of find_elements
This method is designed to find all elements matching a locator, not just one.
Step 2: Identify the return type
It returns a list of WebElement objects, even if the list is empty when no elements match.
Final Answer:
A list of WebElement objects matching the locator -> Option A
Quick Check:
find_elements returns list [OK]
Hint: Remember: plural 'elements' means a list, not one item [OK]
Common Mistakes:
Confusing find_element (single) with find_elements (multiple)
Expecting a single WebElement instead of a list
Thinking it returns a count or boolean
2. Which of the following is the correct syntax to find all buttons on a page using Selenium Python?
easy
A. driver.find_elements(By.TAG_NAME, 'button')
B. driver.find_element(By.TAG_NAME, 'button')
C. driver.find_elements('button')
D. driver.find_element('button')
Solution
Step 1: Use the correct method for multiple elements
To find all buttons, use find_elements, not find_element.
Step 2: Use the correct locator syntax
The method requires two arguments: locator strategy and locator value, e.g., By.TAG_NAME and 'button'.
Final Answer:
driver.find_elements(By.TAG_NAME, 'button') -> Option A
Quick Check:
Correct method and locator = B [OK]
Hint: Use find_elements with By strategy and locator tuple [OK]
Common Mistakes:
Using find_element instead of find_elements
Passing only one argument instead of two
Using string locator without By class
3. What will be the output of this code snippet if the page has exactly 3 <li> elements?