How to find Elements by Class Name using Selenium?

Find Elements by Class Name

To find HTML Elements by class name attribute using Selenium in Python, call find_elements() method, pass By.CLASS_NAME as the first argument, and the class name (of the HTML Elements we need to find) as the second argument.

find_elements(By.CLASS_NAME, "class_name_value")

find_elements() method returns all the HTML Elements that match the given class name as a list.

If there is no elements by given class name, find_elements() function returns an empty list.


Consider the HTML document at the URL, with the following HTML content.

HTML Webpage

  <p>Paragraph 1</p>
  <div class="flat-button">Button 1</div>
  <div class="flat-button">Button 2</div>
  <div class="card">Card 1</div>
  <div class="card">Card 2</div>

In the following program, we will find all the HTML elements whose class name is 'flat-button', using find_elements() method, and print those elements to the console.

Python Program (Selenium)

from selenium import webdriver
from import ChromeDriverManager
from import Service as ChromeService
from import By

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url

# Find elements whose Class Name = "flat-button"
my_elements = driver.find_elements(By.CLASS_NAME, 'flat-button')
for element in my_elements:

# Close the driver


<div class="flat-button">Button 1</div>
<div class="flat-button">Button 2</div>


In this Python Selenium tutorial, we learned how to find all the elements by class name, present in the webpage, using Selenium.

Related Tutorials

Quiz on Selenium

Q1. What is a web driver in Selenium Python?

Not answered

Q2. Which of the following is not a method to wait for a web element to load in Selenium Python?

Not answered

Q3. Which of the following is not a commonly used assertion method in Selenium Python?

Not answered

Q4. What is the method used to switch to a different frame in Selenium Python?

Not answered

Q5. What is the method used to clear the text from an input field in Selenium Python?

Not answered
Code copied to clipboard successfully 👍