Xpath学习笔记

XPATH -> XML path language

Node

In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document nodes.

XML documents are treated as trees of nodes. The topmost element of the tree is called the root element.

Relationship of Nodes

  • Parent

Each element and attribute has one parent.

  • Children

Element nodes may have zero, one or more children.

  • Siblings

Nodes that have the same parent.

  • Ancestors

A node’s parent, parent’s parent, etc.

  • Descendants

A node’s children, children’s children, etc.

Selecting nodes

XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps. The most useful path expressions are listed below:

1
2
3
4
5
6
7
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

Examples

1
2
3
4
5
6
bookstore	        Selects all nodes with the name "bookstore"
/bookstore Selects the root element bookstore
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

Xpath Axes

https://www.w3schools.com/xml/xpath_syntax.asp

Contains, Siblings

https://www.guru99.com/using-contains-sbiling-ancestor-to-find-element-in-selenium.html

Simple online Xpath test

http://www.xpathtester.com/xpath/76bb0bca-1896-43b7-8312-54f924a98a89

打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2019-2022 Chenhao Li

请我喝杯咖啡吧~

支付宝
微信