html-agility-pack

How to get html elements with multiple css classes


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?


Solution

  • The expression you're looking for is:

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

    There are multiple XPath visualisation tools online that can greatly help test any expression.