Common xpath


1. xpath syntax

Just take a look at the novice tutorial

1. Basic grammar

XPath uses path expressions to select nodes in XML documents. Nodes are selected by following a path or step. The most useful path expressions are listed below:

expression describe
nodename Select all child nodes of this node.
/ Select from the root node (take child nodes).
// Selects nodes in the document from the current node matching the selection, regardless of their position (taking descendant nodes).
. Select the current node.
Select the parent node of the current node.
@ Select attributes.

2. Basic grammar - Predicates

Predicates are used to find a specific node or nodes that contain a specified value.

Predicates are enclosed in square brackets.

In the table below, we list some path expressions with predicates, and the results of the expressions:

path expression result
/bookstore/book[1] Selects the first book element that is a child of bookstore.
/bookstore/book[last()] Selects the last book element that is a child of the bookstore element.
/bookstore/book[last()-1] Selects the penultimate book element that is a child of the bookstore element.
//title[@lang] Selects all title elements that have an attribute named lang.
//title[@lang=‘eng’] Selects all title elements that have a lang attribute with a value of eng.



☺ 2. Common scenarios of project xpath

1. Selection of multiple identical tags

(1) contains () method: a tag that contains a certain content

--举例1:
xpath("//div[@class='row']/div[contains(@class, 'search-results')]")
xpath("//p//strong[contains(text(), "Release date")]")
  • Example 2:

(2) Get the sub-node through the node serial number

--举例1:
xpath("//div[@class='result'][1]/h1/a/text()")
  • Example 2:

(3) following-sibling:: Get the label of the same level

--举例1:
xpath("//p//strong[contains(text(), "Release date")]//following-sibling::i[1]")
  • Example 2:

Our company's project introduces the version of xpath dependency. Its parsing of xpath is a little different from what Google Chrome sees. For example, following-sibling::a, Google will get all a tags, but in the project, Only take the first a tag.


2. Commonly used methods and predicates in projects

text() gets the text content

@Attributes such as @class, @href @title

following-sibling gets sibling elements

The contains method selects after judging multiple identical elements.

node() gets all nodes

count method calculates quantity

position method determines position

  • Example:
xpath2("//p//strong[contains(text(), "Genre:")]//following-sibling::a[position()<(count(//p//strong[contains(text(), "Genre:")]//following-sibling::node())-count(//p//strong[contains(text(), "Genre:")]//following-sibling::br[1]//following-sibling::node()))*0.5]")

If an error is reported when using xpath, use xpath2. The dependency of xpath2 is introduced in the company project.


3. xpath string related functions

It’s similar to some functions of Java’s String!

Example of splicing string concat method

xpath("//div[@class='row']/div[@class='search-results']/div[@class='result'][1]/h1/a/text()")


xpath2("concat('本周Steam值得关注的游戏',//div[@class='row']/div[contains(@class, 'search-results')]/div[@class='result'][1]/h1/a/text())")

Determine whether there is a string contains method



3. Commonly used tools for writing xpath—Google Chrome

Example:




If this article is helpful to you, please remember to give Yile a like, thank you!

Guess you like

Origin blog.csdn.net/weixin_45630258/article/details/129257347