XPath使用语法

 

由于python制做自动化脚本网页须要定位到相关元素html

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

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

xpath

它是什么spa

语法:code

Expression Description
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

 

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

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

示例:htm

 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 Expression Result
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!blog

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”ip

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

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元素
相关文章
相关标签/搜索