xpath get element by index

后端 未结 2 402

I have the below xpath expression

//div[@class=\"post-content\"]//img

which runs on a html page, scanning for images. The above query retur

相关标签:
2条回答
  • 2020-12-31 02:07

    In XPath index starts from 1 position, therefore

    //div[@class="post-content"]//img[2]
    

    should work correctly if you have to select each 2nd img in div[@class="post-content"]. If you have to select only 2nd img from all images that are in div[@class="post-content"], use:

    (//div[@class="post-content"]//img)[2]
    
    0 讨论(0)
  • 2020-12-31 02:16

    indexes in XPath are 1-based, not 0-based. Try

    (//div[@class="post-content"]//img)[position()=2]
    
    0 讨论(0)
提交回复
热议问题