0
0
Selenium-pythonHow-ToBeginner · 3 min read

How to Find Element by Class Name in Selenium

In Selenium, you can find an element by its class name using driver.find_element(By.CLASS_NAME, "class_name"). This method locates the first element matching the given class name on the web page.
📐

Syntax

The syntax to find an element by class name in Selenium is:

  • driver.find_element(By.CLASS_NAME, "class_name") - finds the first element with the specified class name.
  • driver.find_elements(By.CLASS_NAME, "class_name") - finds all elements with the specified class name and returns a list.

Here, driver is your Selenium WebDriver instance, and By.CLASS_NAME is the locator strategy.

python
from selenium import webdriver
from selenium.webdriver.common.by import By

# Example syntax to find a single element by class name
element = driver.find_element(By.CLASS_NAME, "example-class")

# Example syntax to find multiple elements by class name
elements = driver.find_elements(By.CLASS_NAME, "example-class")
💻

Example

This example opens a webpage, finds an element by its class name, and prints its text content.

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import time

# Setup Chrome WebDriver (adjust path to your chromedriver)
options = Options()
options.add_argument('--headless')  # Run browser in headless mode
service = Service(executable_path="./chromedriver")
driver = webdriver.Chrome(service=service, options=options)

try:
    driver.get("https://www.example.com")
    # Find element by class name
    element = driver.find_element(By.CLASS_NAME, "example")
    print("Element text:", element.text)
finally:
    driver.quit()
Output
Element text: Example Domain
⚠️

Common Pitfalls

  • Multiple classes: If the element has multiple classes, use only one class name without spaces.
  • Class name with spaces: Selenium does not accept multiple class names separated by spaces; use CSS selectors instead.
  • Element not found: Make sure the element is present and visible before searching, or use waits.
python
from selenium.webdriver.common.by import By

# Wrong: Using multiple class names separated by space
# element = driver.find_element(By.CLASS_NAME, "class1 class2")  # This will fail

# Right: Use one class name or CSS selector
# element = driver.find_element(By.CSS_SELECTOR, ".class1.class2")
📊

Quick Reference

Use By.CLASS_NAME to locate elements by a single class name. For multiple classes, prefer CSS selectors. Always handle cases where elements may not be immediately available using waits.

MethodDescriptionExample
find_element(By.CLASS_NAME, "class")Find first element with classdriver.find_element(By.CLASS_NAME, "btn-primary")
find_elements(By.CLASS_NAME, "class")Find all elements with classdriver.find_elements(By.CLASS_NAME, "item")
find_element(By.CSS_SELECTOR, ".class1.class2")Find element with multiple classesdriver.find_element(By.CSS_SELECTOR, ".btn.primary")

Key Takeaways

Use driver.find_element(By.CLASS_NAME, "class_name") to find the first element by class name.
Only use one class name without spaces; for multiple classes, use CSS selectors.
Handle element presence with waits to avoid errors.
find_elements returns a list of all matching elements.
Always quit the driver to close the browser after tests.