How to Find Elements in Selenium: A Beginner’s Guide with Examples
SeleniumLearn how to find elements in Selenium using Python. Discover locator types, code examples, and tips for locating web elements on any web page.

Vilius Dumcius
Key Takeaways
-
The find_element() and find_elements() methods are essential in Selenium projects, with key differences in how they return results.
-
Use locator types like IDs, CSS selectors, or names, depending on the HTML structure of the web page.
-
For multiple elements, use loops to handle every web element, especially when the elements match your query.
In Selenium, locating web elements is one of the most critical steps. It’s how you tell your script where to click, what to read, or what to type into. You need to be precise and clear to find element targets on a web page.
When you locate an element, Selenium returns a web element object. It can be a button, a link, a text box, or anything else that you can find. Then, you can interact with it. It’s an essential part of all Selenium tasks, and everything depends on it working well.
We’ll walk you through how to find element targets with Selenium WebDriver, the types of locators you can use, and how to handle multiple elements on a page. Additionally, we’ll give you code snippet examples for you to follow along.
Selenium’s “find_element” and “find_elements” Methods
Selenium offers two main tools: find_element() and find_elements(). These are used for locating web elements on a web page:
- find_element(). Gets the first matching element it finds.
- find_elements(). Returns a list of all matching elements.
The syntax looks like:
element = driver.find_element(By.ID, "username")
elements = driver.find_elements(By.CLASS_NAME, "product-title")
When you use find_element(), you get a single web element. If no match is found, Selenium throws an error.
When you use find_elements(), it gives back a list of web elements in Selenium. If nothing matches, you get an empty list. It makes find_elements() safer when you expect several elements or aren’t sure if any elements match.
Use find_element() when you only care about the first matching element. Use find_elements() to locate multiple elements, like product titles and buttons.
Common Locator Types in Selenium WebDriver
A locator tells Selenium how to find element targets on a web page. You point to where it should go, and it follows your instructions. There are many different locator types, but here are some of the most common ones.
1. ID Attribute
The ID is the most reliable locator. It should be unique on every web page.
driver.find_element(By.ID, "search-input")
2. Name Attribute
If the ID attribute is missing, you can try using the name attribute.
driver.find_element(By.NAME, "email")
3. Class Name
Use the class name if the element has a class, but be careful. It’s an attribute that’s not always unique, and you may get duplicates.
driver.find_element(By.CLASS_NAME, "submit-button")
4. Tag Name
It grabs web elements in Selenium by their HTML tag. It’s useful when you want to find multiple elements like all <a> or <button> tags.
driver.find_elements(By.TAG_NAME, "a")
5. CSS Selector
A CSS selector can grab items using class, ID, or other attributes. It’s a flexible method that lets you target complex structures in the HTML.
driver.find_element(By.CSS_SELECTOR, "input[type='text']")
6. XPath Expression
An XPath expression lets you navigate the tree-like XML document structure of a page. Use XPath when other locators don’t work well.
driver.find_element(By.XPATH, "//div[@class='login-form']//input")
How to Use “find_element” in Python
Before we begin, you’ll need to download Selenium, which won’t even take long. With Selenium 4.6+, you don’t need to download chromedriver manually. Chrome WebDriver will automatically download and configure the correct driver version.
Here is a simple Python script using different locators. It should open a page, grab some elements in Selenium, and print them.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.saucedemo.com")
wait = WebDriverWait(driver, 10)
username_input = wait.until(EC.presence_of_element_located((By.ID, "user-name")))
print(f"Username placeholder: {username_input.get_attribute('placeholder')}")
password_input = driver.find_element(By.ID, "password")
print(f"Password placeholder: {password_input.get_attribute('placeholder')}")
login_btn = driver.find_element(By.ID, "login-button")
print(f"Login button text: {login_btn.get_attribute('value')}")
logo = driver.find_element(By.CLASS_NAME, "login_logo")
print(f"Logo text: {logo.text}")
driver.quit()
Each find_element() grabs a different web element. All you need to do is download Selenium 4.6+, go to the web page, grab what you need, and then do what you need to do with it.
Finding Multiple Elements With “find_elements”
If you’re working with web pages that contain lists, for example, product cards, news articles, or buttons, you often need to work with several elements. find_elements() is the function you need to use for that.
It returns a list of web elements in Selenium that match your locator. Then, you can loop through them to read or interact with each one.
Let’s say you want to collect product titles and links from an online store. Each product has a title inside a <div> with class “product-title” and an anchor tag with the link.
You can locate multiple elements and loop through them like this:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get("https://iproyal.com/proxies/")
time.sleep(3)
product_names = driver.find_elements(By.CSS_SELECTOR, "p.tp-headline-s.mb-24.mt-10")
product_links = driver.find_elements(By.CSS_SELECTOR, "a.animated-icon-trigger-hover")
for i in range(len(product_names)):
print("Product:", product_names[i].text)
print("Link:", product_links[i].get_attribute("href"))
print("-" * 30)
driver.quit()
It uses both CLASS_NAME and CSS_SELECTOR to find matching elements. Each web element is printed with its text and link.
Here’s an additional quick code snippet for when you want to collect listing buttons on a web page:
buttons = driver.find_elements(By.TAG_NAME, "button")
for button in buttons:
print("Button Text:", button.text)
It grabs all button tags from the HTML and prints their labels. If none of the elements match, the list simply comes back empty instead of crashing.
To ensure you’re always on the right track when finding elements, keep these tips in mind:
- Use find_elements() when you’re sure there are more than 1 element to collect.
- Always loop through the returned list. It may contain 0, 1, or many web elements.
- Combine get_attribute() with each web element to grab details like text, links, or classes.
It helps with locating web elements in bulk during Selenium projects. It’s also key when scraping lists from a web page or a web application, especially when you need to find multiple web elements that follow the same layout.
Tips for Better Element Location
To get better results when finding web elements, follow these best practices:
- Always start with the ID, if available.
- Avoid over-relying on class names if they’re shared.
- Use CSS selectors for complex layouts.
- Check the HTML structure of the web page using Chrome DevTools.
- Use tools like SelectorsHub, ChroPath, or built-in browser inspection tools.
It should help you find more stable and accurate paths, so your Selenium scripts break less often.