How can I find an element by CSS class with XPath?

HtmlCssXmlXpath

Html Problem Overview


In my webpage, there's a div with a class named Test.

How can I find it with XPath?

Html Solutions


Solution 1 - Html

This selector should work but will be more efficient if you replace it with your suited markup:

//*[contains(@class, 'Test')]

Or, since we know the sought element is a div:

//div[contains(@class, 'Test')]

But since this will also match cases like class="Testvalue" or class="newTest", @Tomalak's version provided in the comments is better:

//div[contains(concat(' ', @class, ' '), ' Test ')]

If you wished to be really certain that it will match correctly, you could also use the normalize-space function to clean up stray whitespace characters around the class name (as mentioned by @Terry):

//div[contains(concat(' ', normalize-space(@class), ' '), ' Test ')]

Note that in all these versions, the * should best be replaced by whatever element name you actually wish to match, unless you wish to search each and every element in the document for the given condition.

Solution 2 - Html

Most easy way..

//div[@class="Test"]

Assuming you want to find <div class="Test"> as described.

Solution 3 - Html

The ONLY right way to do it with XPath :

//div[contains(concat(" ", normalize-space(@class), " "), " Test ")]

The function normalize-space strips leading and trailing whitespace, and also replaces sequences of whitespace characters by a single space.


Note

If not need many of these Xpath queries, you might want to use a library that converts CSS selectors to XPath, as CSS selectors are usually a lot easier to both read and write than XPath queries. For example, in this case, you could use the selector div.Test to get the exact same result.

Some libraries I've been able to find :

Solution 4 - Html

I'm just providing this as an answer, as Tomalak provided as a comment to meder's answer a long time ago

//div[contains(concat(' ', @class, ' '), ' Test ')]

Solution 5 - Html

XPath has a contains-token function, specifically designed for this situation:

//div[contains-token(@class, 'Test')]

It's only supported in the latest version of XPath (3.1) so you'll need an up-to-date implementation.

Solution 6 - Html

Since XPath 2.0 there is a tokenize-function you can use:

//div[tokenize(@class,'\s+')='Test']

Here it will tokenize on white-space and then compares the resulting strings with 'Test'.

It's an alternative of the XPath 3.1 function contains-token()

But at this moment (2021-04-30) no browser support XPath 2.0 or more.

Solution 7 - Html

//div[@class[contains(.,'Test')]]

This is what I am using in my current project and it works smooth as.

The dot . in the expression represents the value of class attribute of any div element. So you don't need to use normalize-space and concat. Note this might also select divs with classnames XXXTestXXX. I happen to have my searchable class as infobox-header and the page doesn't have anything like XXinfobox-headerXXXX.

Solution 8 - Html

Match against one class that has whitespace.

<div class="hello "></div>
//div[normalize-space(@class)="hello"]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStrawberryView Question on Stackoverflow
Solution 1 - Htmlmeder omuralievView Answer on Stackoverflow
Solution 2 - HtmlOlli PuljulaView Answer on Stackoverflow
Solution 3 - HtmlJohn SlegersView Answer on Stackoverflow
Solution 4 - HtmlAlex LymanView Answer on Stackoverflow
Solution 5 - HtmlBennett McElweeView Answer on Stackoverflow
Solution 6 - HtmlSiebe JongebloedView Answer on Stackoverflow
Solution 7 - Htmluser31782View Answer on Stackoverflow
Solution 8 - HtmlPhilipView Answer on Stackoverflow