In my webpage, there's a div with a class named Test.
How can I find it with XPath?
In my webpage, there's a div with a class named Test.
How can I find it with XPath?
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.
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.
//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.
Match against one class that has whitespace.
<div class="hello "></div>
//div[normalize-space(@class)="hello"]