Mastering XPath Class: From Basics to Advanced Use Cases
SeleniumLearn how to master the XPath class in web automation. Understand the class attribute, build accurate XPath expressions, and handle multiple classes in Selenium.

Justas Vitaitis
Key Takeaways
-
The class attribute is a primary way to locate items on a webpage.
-
Use contains or the space normalization trick to avoid matching the wrong buttons.
-
Stick to By.CLASS_NAME for easy tasks, and use XPath for more complicated ones.
Web developers frequently need to locate specific parts of a webpage for testing or scraping. XPath offers a precise way to do it by targeting the class name attribute of an HTML element. It provides control when you need to select items based on their style grouping.
Sometimes, standard tools just don’t work well enough, so you have to use a stronger approach to get the job done.
What Is XPath and Why It Matters
XPath is a query language that navigates through XML or HTML documents . It acts like a map that leads you to specific nodes in the data structure. You can select elements by class by looking at their tags, text, or attributes. It makes finding the needed data significantly simpler.
Web scraping and automation rely heavily on this location strategy. You might need to grab a list of prices or click a specific button automatically.
Since websites use class name attributes to design their pages, targeting these classes is very effective. It helps you find and select elements even when they lack a unique ID or name.
Understanding the XPath Class Attribute
An HTML class attribute often holds a list of words separated by spaces. These words describe how the element looks or acts on the screen. When you write an XPath query, you refer to it as the @class attribute, which is a label that tells the browser what to do with that specific box.
A command like //div[@class=’foo’] checks for that exact text string. However, an element might have a value like class=”btn primary large”. You must choose if you want to match the whole string or just one word. Matching a single class usually requires the contains function to work correctly.
How to Select Elements by Class in XPath
You can use a few standard patterns to find exactly what you need:
- Exact match on class name: //div[@class=’my-class’]
- Contains a class when multiple classes exist: //div[contains(@class, ‘my-class’)]
- More precise partial matching among multiple classes: //div[contains(concat(’ ‘, normalize-space(@class), ’ ‘), ’ my-class ‘)]
Keep in mind that the contains pattern is prone to partial matches since it can accidentally match class=”recorder” or class=”border” if you search for contains(@class, ‘order’).
Common Mistakes and How to Fix Them
Errors happen when you search for class=’btn large’, but the website code is class=”large btn”. It fails because the order of words is different. You can fix it by asking if the attribute contains the word, rather than equaling it. It can be annoying when a simple text swap breaks your whole script.
Another issue occurs when you search for “btn” and accidentally match “btn-secondary”. It creates a false positive because the computer sees the partial match.
The best fix is to add spaces around the class name in your code. It will force the system to look for the whole word “btn” instead of just part of it.
XPath Class in Selenium
Selenium allows you to use these XPath strategies directly in your automation scripts. Here is how you can do it in Python:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://iproyal.com")
h1 = driver.find_element(By.XPATH, "//h1[contains(@class, 'title-gradient-color')]")
print(f"H1: {h1.text}")
buttons = driver.find_elements(By.XPATH, "//div[contains(@class, 'mt-32')]//a")
for btn in buttons:
print(f"Button: {btn.text} -> {btn.get_attribute('href')}")
driver.quit()
Using By.XPATH() vs By.CLASS_NAME
Selenium offers a specific locator strategy called By.CLASS_NAME. It’s a method that works fast if the selected element has only one simple class name. However, note that if you try to pass compound classes with spaces, like “btn primary”, it will raise an InvalidSelectorException error.
The By.XPATH strategy solves the limitation completely. You can build complex logic to handle multiple classes or check other attributes at the same time. Use By.CLASS_NAME for quick, unique elements. Switch to correct XPath expressions when you need to navigate through complex or messy code.
Advanced XPath Class Use Cases
You can ask for two different things in the same line of code. The and operator ensures an element has both specific classes:
//div[contains(@class, 'btn') and contains(@class, 'active')]
You might also just need one of the options to be true:
//div[contains(@class, 'btn-primary') or contains(@class, 'btn-secondary')]
Such flexibility lets you catch and select elements even if their state changes.
Nested Class Matching Examples
HTML documents often put boxes inside other boxes in a deep structure. You might need to find a span text inside a specific div element container:
<div class="container">
<div class="panel active">
<span class="title">Hello</span>
</div>
</div>
You can then write a path that walks down this tree:
//div[@class='container']//div[contains(@class, 'panel') and contains(@class, 'active')]/span[@class='title']
It targets the span only when it sits inside those specific parents.
Dynamic Elements and Attribute Combinations
Some websites change their class names automatically when the page loads. You can mix a class check with a stable data attribute to find them:
//div[contains(@class, 'item-') and @data-id='1234']
You can also look for an element based on its neighbors:
//div[contains(@class,'card')][@role='group']//a[contains(@class,'btn') and text()='Next']
These advanced tricks act as a safety net for your automation.
Conclusion
Mastering the @class technique gives you better control over web elements. You can handle tricky lists of classes or exact matches with confidence. Your automation scripts should stop breaking so often when you write them this way. Strong selectors make your work easier, faster, and much more reliable.
While XPath is powerful , for simple class matching, CSS selectors (driver.find_element(By.CSS_SELECTOR, ".btn.primary")) are often cleaner and faster. Use XPath when you need to match based on text content or parent elements.