How to match on an attribute that contains a certain string

xpath

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>?

Best Answer

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.