如何使用 XPath 按位置 select 多个元素?

How to select multiple elements by position with XPath?

我在以下网站上有 selects 元素的以下 XPath 代码:

XPath 代码:

//div[@title='buyer-info']

网站:http://econpy.pythonanywhere.com/ex/001.html

到select返回元素的第一个元素,我会用:

(//div[@title='buyer-info'])[1]

我想要select倒数第4个元素。我想 XPath 代码应该是这样的:

(//div[@title='buyer-info'])[4:last()]

但这不是有效的 XPath 查询。

您只需检查 position() 值:

The position function returns a number equal to the context position from the expression evaluation context.

(//div[@title='buyer-info'])[position() >= 4]