一、//table[@id=“tableServicesList”]//span[@class=“label status status-active”]/ancestor::tr//[@class=“responsive-edit-button”]/a/@href*
解释:
1. //
前节点之后不论路径
2. //table[@id=“tableServicesList”]
id属性值为tableServicesList的table标签
注意:属性前面要加@; table可以换成通配符*
3. span[@class=“label status status-active”]/ancestor::tr
查找到的span节点的所有tr标签的祖父节点(该知识点是Xpath的轴)
4. a/@href
a节点的href属性的值
二、//header[text()[contains(. ,‘雷猴呀’)]]/parent::section
解释
文本包含 ‘雷猴呀’ 字符串的header标签的section标签的父亲节点
注意:
- text()前面不要加@.
- 属性要这样用://header[contains(@data-params, “嘿嘿嘿”)]
- 中文网上text()的contains用法全是错的,或者chrome不支持
延伸
XPath 2.0 中支持以lower-case():方式进行大小写不敏感的操作:
/html/body//text()[contains(lower-case(.),‘test’)]
XPath 1.0 中用小写搜索’test’较为麻烦:
//header[text()[contains(translate(., ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’, ‘abcdefghijklmnopqrstuvwxyz’), ‘test’)]]