I have several nodes with some particular attribute and I need to select one of them by index. For example I need to select second <div> with ‘test’ class – //div[@class='test'][2] doesn’t work.
Is there a way to select node with some attribute by index ? How to do it?
This is a FAQ.
In XPath the
[]operator has a higher precedence (binds stronger) than the//pseudo-operator.Because of this, the expression:
selects all
divelements whoseclassattribute is “test” and who (thedivelements) are the second suchdivchild of their parent. This is not what you want.Use: