Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

Locating web elements by class name in Selenium is a fundamental technique used for interacting with elements on a web page. Selenium provides a method called ‘find_element_by_class_name‘ to locate a single web element and ‘find_elements_by_class_name‘ to locate multiple elements that share the same class name.

Basics of Locating by Class Name

Single Element

To locate a single web element by class name, you use the ‘find_element_by_class_name‘ method. This method returns the first web element with the specified class name.

Multiple Elements

To locate multiple web elements by class name, you use the ‘find_elements_by_class_name‘ method. This method returns a list of web elements that match the specified class name.

Syntax

				
					# Import the necessary modules
from selenium import webdriver

# Initialize the WebDriver
driver = webdriver.Chrome()

# Open a webpage
driver.get('https://example.com')

# Locate a single element by class name
element = driver.find_element_by_class_name('example-class')

# Locate multiple elements by class name
elements = driver.find_elements_by_class_name('example-class')

# Close the WebDriver
driver.quit()

				
			

Detailed Examples

Example 1: Locating a Single Element by Class Name

				
					from selenium import webdriver

# Initialize the WebDriver
driver = webdriver.Chrome()

# Open a webpage
driver.get('https://example.com')

# Locate a single element by class name
element = driver.find_element_by_class_name('example-class')

# Perform actions on the located element
print(element.text)
element.click()

# Close the WebDriver
driver.quit()

				
			

In this example:

  1. The WebDriver is initialized and navigates to ‘https://example.com'.
  2. The ‘find_element_by_class_name‘ method is used to locate the first element with the class name ‘example-class‘.
  3. The text of the element is printed, and a click action is performed on the element.
  4. The WebDriver is closed.

Example 2: Locating Multiple Elements by Class Name

				
					from selenium import webdriver

# Initialize the WebDriver
driver = webdriver.Chrome()

# Open a webpage
driver.get('https://example.com')

# Locate multiple elements by class name
elements = driver.find_elements_by_class_name('example-class')

# Perform actions on the located elements
for element in elements:
    print(element.text)
    # You can perform other actions on each element if needed

# Close the WebDriver
driver.quit()

				
			

In this example:

  1. The WebDriver is initialized and navigates to ‘https://example.com'.
  2. The ‘find_elements_by_class_name‘ method is used to locate all elements with the class name ‘example-class‘.
  3. A loop iterates through the list of elements, printing the text of each element.
  4. The WebDriver is closed.

Handling Multiple Classes

If an element has multiple class names, you can only search by one class name at a time. Selenium does not support searching for elements with multiple class names directly using ‘find_element_by_class_name or find_elements_by_class_name‘. Instead, you can use CSS selectors for more complex searches.

Example: Using CSS Selector for Multiple Classes

				
					from selenium import webdriver

# Initialize the WebDriver
driver = webdriver.Chrome()

# Open a webpage
driver.get('https://example.com')

# Locate an element with multiple classes
element = driver.find_element_by_css_selector('.class1.class2')

# Perform actions on the located element
print(element.text)
element.click()

# Close the WebDriver
driver.quit()

				
			

In this example:

  1. The WebDriver is initialized and navigates to ‘https://example.com'.
  2. The ‘find_element_by_css_selector‘ method is used to locate an element that has both ‘class1‘ and ‘class2‘ as its class names.
  3. The text of the element is printed, and a click action is performed on the element.
  4. The WebDriver is closed.

Best Practices

  • Ensure that the class name you are using is unique to the element you want to locate to avoid ambiguity.
  • Handle exceptions such as NoSuchElementException and ElementNotVisibleException to make your code more robust.
  • Use CSS selectors or XPath if you need to locate elements with more complex criteria.

Deprecated Methods Notice

Note that ‘find_element_by_class_name‘ and ‘find_elements_by_class_name‘ have been deprecated in the latest versions of Selenium (v4 and later). The new syntax uses the ‘By‘ class and ‘find_element/find_elements‘ methods.

Updated Syntax for Selenium 4

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

# Initialize the WebDriver
driver = webdriver.Chrome()

# Open a webpage
driver.get('https://example.com')

# Locate a single element by class name
element = driver.find_element(By.CLASS_NAME, 'example-class')

# Locate multiple elements by class name
elements = driver.find_elements(By.CLASS_NAME, 'example-class')

# Close the WebDriver
driver.quit()

				
			

In summary, locating elements by class name in Selenium is straightforward and useful for web automation tasks. Using the correct methods and handling exceptions appropriately can enhance the reliability and maintainability of your Selenium scripts.

Scroll to Top