Find the nth child under svg using xpath

2019-08-08 12:09发布

I am writing xpath for elements present under svg tag. I want to get the 2nd rect node.

My xpath finds all 3 nodes, whereas my requirement is to find only the 2nd -

xpath: //*[name()='svg']/*[name()='rect' and @fill='#919EF9']

HTML sample,

<svg width="400" height="110">
<rect id="yui_3_6_0_1_1435305312018_1190" x="35.5" y="60.5" width="68" height="117" stroke="#FFFFFF" stroke-width="1" fill="#919EF9" rx="0" ry="0">
<rect x="176.5" y="65.5" width="68" height="48" stroke="#FFFFFF" stroke-width="1" fill="#919EF9" rx="0" ry="0">
<rect x="316.5" y="30.5" width="68" height="70" stroke="#FFFFFF" stroke-width="1" fill="#919EF9" rx="0" ry="0">
</svg>

2条回答
淡お忘
2楼-- · 2019-08-08 13:02

Use [n] to select the nth element:

//*[name()='svg']/*[name()='rect' and @fill='#919EF9'][2]
查看更多
混吃等死
3楼-- · 2019-08-08 13:10

use following XPATH

//svg/rect[2]
查看更多
登录 后发表回答