当前位置:网站首页>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
边栏推荐
- How can Plato obtain premium income through elephant swap in a bear market?
- 《LOL》从代码上来说最难的是哪个英雄?
- Network security (5)
- Implementation and verification logic of complex expression input component
- 这是一份不完整的数据竞赛年鉴!
- ORBSLAM2安装测试,及各种问题汇总
- TCP failure model
- Unity3d hodgepodge
- On contract testing
- Mysql database final review question bank
猜你喜欢

Implementation and verification logic of complex expression input component

Zhongang Mining: four steps for sustainable and healthy development of fluorite industry

SiC功率半导体产业高峰论坛成功举办

Excel tool for generating database table structure

7.9-7.17 new features and grammar of learning plan ES6
![[wechat applet] interface generates customized homepage QR code](/img/9b/cccdb8ff6db61518402a27b94d0196.png)
[wechat applet] interface generates customized homepage QR code

Appendix 2 – some simple exercises

On memory computing integrated chip technology

深入浅出依赖注入及其在抖音直播中的应用

HarmonyOS 3.0 发布!
随机推荐
Unity3d hodgepodge
Unity3d empty package APK error summary
JS to achieve full screen effect
造型科幻、标配6安全气囊,风行·游艇11.99万起售
PyQt5快速开发与实战 6.1 好软件的三个维度 && 6.2 PyQt5中的布局管理 && 6.3 PyQt5的绝对位置布局
Nutnews developed based on arkui ETS
怎么样的框架对于开发者是友好的?
Pyqt5 rapid development and practice 6.4 qboxlayout (box layout)
Sublime Text3 设置不同文件不同缩进
Four types of technical solutions shared by distributed sessions, and their advantages and disadvantages
系统架构师学习
PyQt5快速开发与实战 6.4 QBoxLayout(框布局)
数据可视化的利器-Seaborn简易入门
Div horizontal layout aligned on both sides
vector实现
Nucleic acid scanning code registration experience (how to improve the correct character recognition rate of OCR)
40余岁的边缘老技术,是未来乏力还是掘地而起?
【C语言】三子棋(智能下棋 + 阻拦玩家)
Sed, regular expression of shell programming
node(二)