xpath 按索引获取元素 [英] xpath get element by index

查看:48
本文介绍了xpath 按索引获取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 xpath 表达式

I have the below xpath expression

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

在 html 页面上运行,扫描图像.上面的查询返回了很多图像,但我只想要列表中的第二个.

which runs on a html page, scanning for images. The above query returns a lot of images but I only want the second in the list.

我尝试过这些,但没有运气:

I have tried these with no luck:

//div[@class="post-content"]//img[1] and
//div[@class="post-content"]//img[position()=1]

推荐答案

在 XPath 中索引从 1 个位置开始,因此

In XPath index starts from 1 position, therefore

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

如果您必须在 div[@class="post-content"] 中选择每个第二个 img

应该可以正常工作.如果您必须从 div[@class="post-content"] 中的所有图像中仅选择第二个 img,请使用:

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]

这篇关于xpath 按索引获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆