XPath使用语法

 

因为python制作自动化脚本网页需要定位到相关元素

方法参考:https://selenium-python.readthedocs.io/getting-started.html

特开一门随笔给使用xpath定位

xpath

它是什么

语法:

ExpressionDescription
nodename Selects all nodes with the name "nodename"
/ Selects from the root node
// Selects nodes in the document from the current node that match the selection no matter where they are
. Selects the current node
.. Selects the parent of the current node
@ Selects attributes

 

表达 描述
节点名称 选择名称为“节点名称”的所有节点
/ 从根节点中选择
// 从当前节点中选择与选择匹配的文档中的节点,无论它们在何处
. 选择当前节点
.. 选择当前节点的父节点
@ 选择属性

下表中列出了一些路径表达式和表达式的结果:

示例:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <bookstore>
 4 
 5 <book>
 6   <title lang="en">Harry Potter</title>
 7   <price>29.99</price>
 8 </book>
 9 
10 <book>
11   <title lang="en">Learning XML</title>
12   <price>39.95</price>
13 </book>
14 
15 </bookstore>

 

 

Path ExpressionResult
bookstore Selects all nodes with the name "bookstore"
/bookstore Selects the root element bookstore

Note: If the path starts with a slash ( / ) it always represents an absolute path to an element!

bookstore/book Selects all book elements that are children of bookstore
//book Selects all book elements no matter where they are in the document
bookstore//book Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element
//@lang Selects all attributes that are named lang

 

路径表达 结果
bookstore 选择名称为“bookstore”的所有节点
/bookstore

选择根元素“bookstore”

注意:如果路径以斜杠(/)开头,它总是表示元素的绝对路径!

bookstore/book

选择作为书店子项的所有书元素

//book 选择所有书籍元素,无论它们在文档中的位置
bookstore//book 选择bookstore元素后代的所有book元素,无论它们在bookstore元素下的哪个位置
//@lang 选择名为lang的所有属性

谓词

用于查找特定节点或包含特定值的节点。

谓词总是嵌在方括号中。

在下表中,我们列出了一些带谓词的路径表达式和表达式的结果:

依旧是以上示例

/bookstore/book[1]

选择第一个book元素作为bookstore元素的子元素。

注意:在IE 5,6,7,8,9中,第一个节点是[0],但根据W3C,它是[1]。要在IE中解决此问题,请将SelectionLanguage设置为XPath:

在JavaScript中:xml.setProperty(“SelectionLanguage”,“XPath”);

/bookstore/book[last()]  选择作为bookstore元素的子元素的最后一个book元素
 /bookstore/book[last()-1]  选择最后一个book元素,它是bookstore元素的子元素
 /bookstore/book[position()<3]  选择作为bookstore元素的子元素的前两个book元素
 //title[@lang]  选择具有名为lang的属性的所有标题元素
 //title[@lang='en']  选择具有值为“en”的“lang”属性的所有标题元素
 /bookstore/book[price>35.00]  选择bookstore元素的所有book元素,其price元素的值大于35.00
 /bookstore/book[price>35.00]/title  选择bookstore元素的book元素的所有title元素,其中price元素的值大于35.00

 

选择未知节点

XPath通配符可用于选择未知的XML节点。

通配符 说明
* 匹配任何元素节点
@* 匹配任何属性节点
node() 匹配任何类型的任何节点

在下表中,我们列出了一些路径表达式和表达式的结果:

 

路径 说明
/bookstore/* 选择bookstore元素的所有子元素节点
//* 选择文档中的所有元素
//title[@*] 选择具有至少一个任何类型属性的所有标题元素

 

 

选择几个路径

路径 表达效果
//book/title | //book/price 选择所有书元素的所有标题和价格元素
//title | //price 选择文档中的所有标题和价格元素
/bookstore/book/title | //price 选择bookstore元素的book元素的所有title元素和文档中的所有price元素

 

posted on 2019-05-14 14:42  HerbieKim  阅读(190)  评论(0编辑  收藏  举报

导航