views:

26

answers:

2

I know how to get a list of DIVs of the same css class e.g

<div class="class1">1</div>
<div class="class1">2</div>

using xpath //div[@class='class1']

But how if a div have multiple classes, e.g

<div class="class1 class2">1</div>

What will the xpath like then?

+1  A: 

Found this blog posts, hope they're useful.

Select HTML elements with more than one css class using XPath

XPath for CSS classes

gcores
Thanks. But seems this doesn't work. rootNode.SelectNodes("//div[(@class='class1') and (@class='class2')]"); returns null
seasong
Yes, sorry I changed that a few minutes ago. Those blog posts deal with the exact problem you're having.
gcores
+1  A: 

The expression you're looking for is:

//div[contains(@class, 'class1') and contains(@class, 'class2')]

I highly suggest XPath visualizer, which can help you debug xpath expressions easily. It can be found here:

http://xpathvisualizer.codeplex.com/

Ioannis Karadimas