I can't clearly understand the differences between using //element
and /descendant::element
when selecting multiple children of a base element in XPath.
Given this HTML snippet
<html>
<body>
<div class="popupContent">
<table>
<tr class="aclass"><td> Hello </td> <td> <input type="text" value="FIRST" /> </td></tr>
<tr class="aclass"><td> Goodbye </td> <td> <input type="text" value="SECOND" /> </td></tr>
</table>
</div>
</body>
</html>
I need to select each input
based on its positioning in the table.
//div[@class='popupContent']//input[1]
this selects the first input
//div[@class='popupContent']//input[2]
this gives error
//div[@class='popupContent']/descendant::input[1]
this again selects the first input
//div[@class='popupContent']/descendant::input[2]
this select the second input
Using /descendant::input
does what I need: grab all inputs and let me select by position.
How does //
differ? Why does it return only the first element and not the ones after?
I'm aware of this other question but the answer basically says they're aliases and point to the documentation, which I cannot understand and lacks a concrete example. Difference with that question is that my need is to select multiple children elements, and //
doesn't allow it.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…