Xpath定位
*1. 模糊匹配 contains
* E.G. <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
input[contains(@class,"s_i")]
所有的input,有class属性,并且属性中带有s_i的节点
*2. id/class/其他属性 定位
E.G. <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
input[@id='kw']
input[@class='s_ipt']
input[@class="s_ipt" and @name="wd"]
*3. starts-with
//input[starts-with(@class,"s")]
所有的input,有class属性,并且属性以s开头
*4. 取文本
<a href="http://tieba.baidu.com" target="_blank" class="mnav c-font-normal c-color-t">XX</a>
//div[@id="s-top-left"]/a[5]/text()获取节点内容
//div[@id="s-top-left"]//text()获取节点里不带标签下所有内容
*5.取href属性
//div[@id="s-top-left"]/a[5]/@href
这篇博客介绍了XPath在网页元素定位中的应用,包括使用contains进行模糊匹配,通过id、class或其他属性精确定位,starts-with用于属性以特定字符串开头的情况,以及如何提取文本和属性值。这些技巧对于网页抓取和自动化测试至关重要。
22万+

被折叠的 条评论
为什么被折叠?



