1. Chrome插件xpath helper可以辅助查看xpath
2. //从当前匹配的节点中选择
3. //book[1] 第一个book元素
//book[last()-1] 倒数第二个book元素
//book[position()<3] 前两个book元素
4. //a[@id] 拥有id属性的a元素
//a[@id="next" and @class="bank"] id属性为next并且class属性为blank的a元素
例1:
获取http://www.w3school.com.cn/xpath/xpath_functions.asp中最下面‘上下文函数’表格中第一行对应的说明的第一行
右键这段话,选择审查元素:
//div[h2//a[@id="context"]]//table//tr[2]//td[2]//p[1]
即可获得:“返回当前正在被处理的节点的 index 位置。”
例2:
找到http://quotes.toscrape.com/中J.K. Rowling说的话:
//div[@class="quote"][span[small="J.K. Rowling"]]//span[@class="text"]
“It is our choices, Harry, that show what we truly are, far more than our abilities.”