How to select elements by class in XPath?

You can select elements by class in XPath by using the contains(@class, "class-name") or @class="class-name" expressions.

The first expression will match any element that contains class-name. Even if the element has additional classes defined it will still match. However, the second expression will match the elements that only have one class named class-name and no additional classes.

Here is some Selenium XPath sample code that extracts the h1 tag from the ScrapingBee website using the class name:

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

DRIVER_PATH = '/path/to/chromedriver'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)

driver.get("https://scrapingbee.com")

# The current HTML of h1 resembles this:
# <h1 class="mb-33">Tired of getting blocked while scraping the web?</h1>
contains_class_match = driver.find_element(By.XPATH, "//h1[contains(@class, 'mb-33')]")

# @class="mb-33" will also work as there is only one class
# defined on the h1 tag
exact_class_match = driver.find_element(By.XPATH, "//h1[@class='mb-33']")

print(contains_class_match.text)
# Output: Tired of getting blocked while scraping the web?

print(exact_class_match.text)
# Output: Tired of getting blocked while scraping the web?

Related XPath web scraping questions: