Selenium – Check if Element has specific class name

Selenium Python – Check if Element has specific Class Name

In this tutorial, you will learn how to check if given element has a specific class name using Selenium, in Python.

To check if a give element has a specific class name in Selenium Python,

  • Find the element
  • Call get_attribute() method of the element object to and pass "class" as argument. The method returns the value for class attribute.
  • Split the value into a list. The list contains all the class names.
  • Check if the required class name is in the list.

Examples

In the following example, we shall consider loading the HTML file at path https://pythonexamples.org/tmp/selenium/index-28.html . The contents of this HTML file is given below. In this webpage, we have a paragraph element with id=”para1″ has two class names: message and intro.

<html>
  <body>
    <h2>Hello User!</h2>
    <p id="para1" class="message intro">This is my <strong>first</strong> paragraph. This is a sample text. Welcome!</p>
  </body>
</html>
Copy

1. Check if the Element has class name “intro”

In the following program, we initialize a webdriver, navigate to a specified URL, get the paragraph element with id="para1", and check if the element has class name intro.

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

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

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-28.html')

# Get the element
the_element = driver.find_element(By.ID, "para1")

# Get the class attribute
class_attr = the_element.get_attribute('class')

# Split into list of class name
class_names = class_attr.split(' ')

# Check if specific class name is present
specific_class_name = 'intro'
if specific_class_name in class_names:
    print('The element has specified class name.')
else:
    print('The element does not have specified class name.')

# Close the driver
driver.quit()

Output

The element has specified class name.

2. Negative Scenario – Check if the Element has class name “dummy”

In the following program, we initialize a webdriver, navigate to a specified URL, get the paragraph element with id="para1", and check if the element has class name dummy.

For the paragraph element that we considered in the webpage, there is no class name dummy. Therefore, the condition in if statement should become False, and else block should run.

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

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

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-28.html')

# Get the element
the_element = driver.find_element(By.ID, "para1")

# Get the class attribute
class_attr = the_element.get_attribute('class')

# Split into list of class name
class_names = class_attr.split(' ')

# Check if specific class name is present
specific_class_name = 'dummy'
if specific_class_name in class_names:
    print('The element has specified class name.')
else:
    print('The element does not have specified class name.')

# Close the driver
driver.quit()

Output

The element does not have specified class name.

Summary

In this Python Selenium tutorial, we have given instructions on how to check if given element has a class name, using get_attribute() method of WebElement class, with example programs.

Related Tutorials

Quiz on Selenium

Q1. What is a web driver in Selenium Python?

Not answered

Q2. Which of the following is not a popular web driver used in Selenium Python?

Not answered

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

Not answered

Q4. What is the method used to navigate back to the previous page in Selenium Python?

Not answered

Q5. What is the method used to take a screenshot in Selenium Python?

Not answered
Code copied to clipboard successfully 👍