How can I match on an attribute that contains a certain string?

I am having a problem selecting nodes by attribute when the attributes contains more than one word. For example:

<div class="atag btag" />

This is my xpath expression:

//*[@class="atag"]

The expression works with

<div class="atag" />

but not for the previous example. How can I select the <div>?

10 Answers
10

Here’s an example that finds div elements whose className contains atag:

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

Here’s an example that finds div elements whose className contains atag and btag:

//div[contains(@class, 'atag') and contains(@class ,'btag')]

However, it will also find partial matches like class="catag bobtag".

If you don’t want partial matches, see bobince’s answer below.

Leave a Comment