当前位置:网站首页>Selenium series 5-xpath path expression
Selenium series 5-xpath path expression
2022-07-29 09:55:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack , I wish every programmer can learn more languages .
Xpath Introduce
- XPath Using path expressions in XML Navigation in the document
- XPath Use path expressions to select XML A node or set of nodes in a document . These path expressions are very similar to those we see in regular computer file systems .
- XPath Contains a library of standard functions
- XPath Contains more than 100 Built in functions . These functions are used for string values 、 The number 、 Date and time comparison 、 Nodes and QName Handle 、 Sequence processing 、 Logical values and so on .
- XPath yes XSLT The main element in
- XPath yes XSLT The main elements of the standard . without XPath Knowledge of , You can't create XSLT file . Can be in 《XSLT course 》 Read more in . XQuery and XPointer All constructed in XPath Above the expression .XQuery 1.0 and XPath 2.0 Share the same data model , And support the same functions and operators . Can be in 《XQuery course 》 Read more about XQuery Knowledge .
- XPath It's a W3C standard
- XPath On 1999 year 11 month 16 Japan Become W3C standard . XPath Designed for XSLT、XPointer And other things XML Parsing software uses . Can be in 《W3C The official tutorial 》 Read more about XPath Standard information
Xpath And HTML contrast
- XML Extensible markup language , Is a subset of the standard General Markup Language ; And HTML similar , But it's not HTML substitute , They are designed for different purposes .
- HTML Designed to display data , The focus is on the appearance of the data .XML Designed to transmit and store data , The focus is on the content of the data .
Xpath The term
node
stay XPath in , There are seven types of nodes : Elements 、 attribute 、 Text 、 Namespace 、 A processing instruction 、 Comments and documentation ( root ) node .XML Documents are treated as node trees . The root of the tree is called the document node or the root node .
Please look at this XML file :
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
above XML Examples of nodes in the document :
<bookstore> ( Document nodes )
<author>J K. Rowling</author> ( Element nodes )
lang="en" ( Attribute node )
Basic value ( Or atomic value ,Atomic value)
The base value is a node without parent or child
above XML Examples of basic values in the document :
J K. Rowling
"en"
Xpath Node relationship
Father (Parent)
Every element and attribute has a parent .
In the following example ,book The element is title、author、year as well as price The father of the element :
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
Son (Children)
Element nodes can have zero 、 One or more children .
In the following example ,title、author、year as well as price The elements are book The child of the element :
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
Compatriot (Sibling)
Nodes with the same parent
In the following example ,title、author、year as well as price All elements are compatriots :
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
Forefathers (Ancestor)
The parent of a node 、 Father's father , wait .
In the following example ,title The ancestor of elements is book Elements and bookstore Elements :
<bookstore>
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
Progeny (Descendant)
The child of a node , Son of son , wait .
In the following example ,bookstore The offspring of book、title、author、year as well as price Elements :
<bookstore>
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
Xpath grammar
XPath Use path expressions to select XML A node or set of nodes in a document . Nodes are created by following paths (path) Or step (steps) To select .
XML Example
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
Select node
XPath Using path expressions in XML Select node in document . The node is either by following the path or step To select . The most useful path expressions are listed below :
expression | describe |
---|---|
nodename | Select all children of this node |
/ | Select from root node ( Take the child node ) |
// | Select the node in the document from the current node that matches the selection , Regardless of their location ( Take the descendant node ) |
. | Select the current node |
.. | Select the parent of the current node |
@ | Select Properties |
In the table below , Some path expressions and the results of the expressions are listed :
Path expression | result |
---|---|
bookstore | selection bookstore All children of the element |
/bookstore | Select the root element bookstore. notes : If the path starts with a forward slash ( / ), This path always represents the absolute path to an element ! |
bookstore/book | Choose to belong to bookstore All of the child elements of book Elements |
//book | Select all book Subelement , Regardless of where they are in the document |
bookstore//book | Choose to belong to bookstore All of the descendants of the element book Elements , No matter where they are bookstore What's down there |
//@lang | Choose the name lang All attributes of |
Predicate (Predicates)
Predicate is used to find a specific node or a node containing a specified value .
The predicate is embedded in square brackets .
In the table below , Lists some path expressions with predicates , And the result of the expression :
Path expression | result |
---|---|
/bookstore/book[1] | Choose to belong to bookstore The first of the child elements book Elements . |
/bookstore/book[last()] | Choose to belong to bookstore The last of the child elements book Elements . |
/bookstore/book[last()-1] | Choose to belong to bookstore The penultimate of a child element book Elements . |
/bookstore/book[position()<4] | Select the first three that belong to bookstore Of a child element book Elements . |
//title[@lang] | Select all owners named lang Property of title Elements . |
//title[@lang=’eng’] | Select all title Elements , And these elements have a value of eng Of lang attribute . |
/bookstore/book[price>35.00] | selection bookstore All of the elements book Elements , And one of them price The value of the element must be greater than 35.00. |
/bookstore/book[price>35.00]//title | selection bookstore In the element book All of the elements title Elements , And one of them price The value of the element must be greater than 35.00. |
Select unknown node
XPath Wildcards can be used to select unknown XML Elements .
wildcard | describe |
---|---|
* | Match any element node . |
@* | Match any attribute node . |
node() | Match any type of node . |
In the table below , Lists some path expressions , And the results of these expressions :
Path expression | result |
---|---|
/bookstore/* | selection bookstore All child elements of the element . |
//* | Select all elements in the document . |
//title[@*] | Select all of the title Elements . |
Select several paths
By using in a path expression ”|” Operator , You can choose several paths .
In the table below , Lists some path expressions , And the results of these expressions :
Path expression | result |
---|---|
//book/title | //book/price | selection book All of the elements title and price Elements . |
//title | //price | Select all... In the document title and price Elements . |
/bookstore/book/title | //price | Choose to belong to bookstore Elemental book All of the elements title Elements , And all the price Elements . |
Reference resources
https://www.runoob.com/xpath/xpath-tutorial.html
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/119797.html Link to the original text :https://javaforall.cn
边栏推荐
- Briefly describe the difference between heap and stack
- 怎么样的框架对于开发者是友好的?
- How to introduce your project experience?
- 《LOL》从代码上来说最难的是哪个英雄?
- Basic operations of OpenCV image processing
- Appendix 2 – some simple exercises
- [C language] minesweeping (recursive expansion + marking function)
- 数据库表结构生成excel工具
- 造型科幻、标配6安全气囊,风行·游艇11.99万起售
- 机器学习之逻辑回归(Logistics Regression)
猜你喜欢
随机推荐
Pytest+allure generate test report
How to query express logistics and filter out no information doc No. to delete or copy
vector实现
[Apple Developer account]06 after transferring the developer account, the annual fee of the developer is automatically renewed
英特尔联合Datawhale,发布学习项目!
TCP failure model
Parameter passing mode of C language (int x) (int *x) (int & x)
Dynamics 365Online 如何自定义商机关闭窗体
引入redis缓存出现的问题以及解决方式
Cloud native management practice: business led Devops continuous delivery system
待人宽容大度
Harmonyos 3.0 release!
上海交大牵手淘宝成立媒体计算实验室:推动视频超分等关键技术发展
Custom configuration
Senparc.Weixin.Sample.MP源码剖析
On memory computing integrated chip technology
Node (II)
How to realize the isolation level between MySQL transactions and mvcc
Functions and arrays
Geeer's happiness | is for the white whoring image! Analysis and mining, NDVI, unsupervised classification, etc