XPath,是在 XSLT 标准中的主要元素,用于在 XML 文档中选择元素,我们可以理解为元素选择器(功能上与 CSS Selectors 类似)。
我们感觉XPath 比 CSS Selector 好用,也可能是我们不熟悉 CSS 选择器,也可能是我们的场景使然。
按照 w3school.com 章节顺序进行学习即可: 1)形成 XPath 基本认识:XPath Tutorial 2)理解基本概念,比如 Parent、Children、Ancestors 等等:XPath Nodes 3)学习 XPath 语法:XPath Syntax 4)相对于当前节点定位其他节点:XPath Axes 5)使用操作符进行判断:XPath Operators 6)学习示例:XPath Examples
我们可以使用 Chrome / Firefox 快速获取元素的 XPath 地址。下面是 Chrome 的方法:
Inspect => Right click on the node => Copy => Copy XPath
在 Chrome / Firefox 的 Console 中,使用 $x('your-xpath-query') 函数,
比如:通过 $x("//img") 选择所有图片
html - XPath:: Get following Sibling - Stack Overflow
获取在 div 的后续所有兄弟(sibling)元素中的 input 元素:
//div/following-sibling::inputXPath with regex match on an attribute value - Stack Overflow
定位 href 属性包含 doubles 字符串的 a 标签:
//a[contains(@href, ' doubles ')]K4NZ / 使用 XPath 选择元素w3schools.com/XPath TutorialXPath - XML Path Language (XPath)Scrapy/Docs » SelectorsIs there a way to get the XPath in Google Chrome? - Stack OverflowHow to validate XPath using Firefox Web Developer Plugin? - Stack Overflow
