How to select elements by class in XPath
XPath gives you three practical ways to target an element by its class attribute, and they behave differently when an element carries several classes or a class name that resembles another.
The choice comes down to whether you want a substring hit, an exact attribute string, or one whole class token inside a multi-class list.
The three class expressions at a glance
The simplest form is contains(@class, "class-name"), which returns any element whose class attribute includes that text anywhere. Because it works on substrings, it also returns elements whose class merely starts with or contains the same characters.
The second form, @class="class-name", compares the entire attribute value. It returns only elements whose class attribute is exactly that string, so an element with extra classes is skipped.
When you need one class token but must tolerate other classes on the same element, use contains(concat(" ", normalize-space(@class), " "), " class-name "). Padding the attribute with spaces on both sides and collapsing whitespace lets the expression look for a space-delimited token. It returns class="class-name active" but not class="class-name-large".
Which expression fits your HTML?
Use this checklist before writing the selector:
- The class attribute is exactly one name and you want only that element:
@class="class-name". - The class attribute may hold several names and you want any element that has this one among them: the concat/normalize-space pattern.
- You deliberately want substring behavior, for example matching a family of related class names:
contains(@class, "class-name"). - You are unsure whether the page uses extra classes: prefer the whole-class pattern, then inspect the returned nodes.
- You are debugging an unexpected extra match: switch from
containsto the whole-class pattern and compare results.
Worked example with three headings
The supplied Selenium script builds a small local HTML page with three h1 elements:
class="mb-[33px]"— one class only.class="mb-[33px] hero-title"— two classes.class="mb-[33px]-large"— a longer class name that begins with the same characters.
Running the three expressions against that page produces distinct result sets:
//h1[contains(@class, 'mb-[33px]')]returns all three headings, because the third one contains the substring.//h1[@class='mb-[33px]']returns only the first heading, because the other two have different attribute strings.//h1[contains(concat(" ", normalize-space(@class), " "), " mb-[33px] ")]returns the first two headings and excludes the third, because the third has no space-delimitedmb-[33px]token.
That contrast is the practical test: if the third heading appears when you did not expect it, your selector is doing substring matching rather than whole-class matching.
Quoting inside the XPath string
The example alternates outer Python quote styles ('...' and "...") so the quotes inside the XPath expression do not need escaping. Both styles are equivalent; pick whichever keeps the inner XPath quotes readable.
| Class attribute | contains(@class, ...) | @class="..." | concat/normalize-space |
|---|---|---|---|
| class="class-name" | match | match | match |
| class="class-name active" | match | no match | match |
| class="class-name-large" | match | no match | no match |
Behavior shown for the exact class-name token used in the expressions; substitute your own class name.
Frequently asked questions
Why does contains(@class, ...) return elements I did not intend to target?
Because it tests for a substring anywhere in the class attribute. An element whose class is class-name-large contains the characters of class-name, so it is returned. Switch to the concat/normalize-space pattern when you need a whole class token.
When should I use @class="..." instead?
Use it when the element's class attribute is exactly that one string and you do not want elements with additional classes. It will not match an element that also carries other class names.
Does the concat/normalize-space pattern handle extra whitespace in the class attribute?
Yes. normalize-space(@class) collapses and trims whitespace, and the surrounding spaces added by concat let the expression look for the class name as a space-delimited token.