selenium xpath positioning

selenium-xpath positioning

<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-javascript">element_xpath <span style=" color:#67cdcc">=</span> driver<span style="color:#cccccc">.</span><span style="color:#f08d49">find_element</span>< span style="color:#cccccc">(</span>By<span style="color:#cccccc">.</span><span style="color:#f8c555">XPATH </span><span style="color:#cccccc">,</span> <span style="color:#7ec699">'xpath expression'</span><span style= "color:#cccccc">)</span>
</code></span></span>

xpath positioning instructions

xpath is XML Path Language, which is specified by the International Organization for Standardization W3C. It is a language used to determine the location of XML document nodes.

xpath positioning advantages

1. Compared with other positioning methods, it can support more positioning methods, such as: Boolean logic judgment, fuzzy positioning, etc.
2. Can support web positioning and Android app native page positioning

xpath positioning shortcomings

1. Need to parse the entire page from beginning to end, which is slow

xpath debugging method

Method 1: In the elements of the browser developer mode, enter the xpath expression in the Ctrl + F search bar
Method 2: In the console of the browser developer mode, the expression can be verified in the following format

<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-javascript"><span style="color :#f08d49">$x</span><span style="color:#cccccc">(</span><span style="color:#7ec699">"xpath expression" </span><span style="color:#cccccc">)</span> # If there are quotation marks in the expression, use single quotation marks, <span style="color:#7ec699">'$ '</span> can be replaced by <span style="color:#7ec699">'$$'</span>
</code></span></span>

xpath node

In XPath, there are seven types of nodes: elements, attributes, text, namespaces, processing instructions, comments, and document nodes (or root nodes)
Before starting the xpath syntax, we need to briefly understand these types of nodes:

Node name Explanation Example
Element node Each tag in the web page Such as ,

Root node The first element node of the web page The root node of a web page is usually …
Attribute node Each attribute in the tag such as

, id is the attribute of the div node

Text node Text of the label For example,

La La La

, ‘La La La’ is the div node Text

xpath syntax

xpath uses path expressions to describe the relationship between the target node and the web page header

Absolute path & relative path

Path expressions have two structures, namely:

  • Absolute path: starting from the root node/html and going down, layer by layer until needed to the node. Taking the Baidu input box as an example, the expression is:
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-javascript"><span style="color :#67cdcc">/</span>html<span style="color:#67cdcc">/</span>body<span style="color:#67cdcc">/</span>div <span style="color:#cccccc">[</span><span style="color:#f08d49">1</span><span style="color:#cccccc">] </span><span style="color:#67cdcc">/</span>div<span style="color:#cccccc">[</span><span style="color:# f08d49">2</span><span style="color:#cccccc">]</span><span style="color:#67cdcc">/</span>div<span style= "color:#cccccc">[</span><span style="color:#f08d49">5</span><span style="color:#cccccc">]</span> <span style="color:#67cdcc">/</span>div<span style="color:#cccccc">[</span><span style="color:#f08d49"> 1</span><span style="color:#cccccc">]</span><span style="color:#67cdcc">/</span>div<span style="color: #67cdcc">/</span>form<span style="color:#67cdcc">/</span>span<span style="color:#cccccc">[</span><span style="color:#f08d49">1</span><span style="color:#cccccc">]</span>
</code></span></span>
  • Relative path: starting from any node, find the required node according to the node description information. Taking the Baidu input box as an example, the expression is:
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-javascript"><span style="color :#999999">//input[@id='su']</span>
</code></span></span>

Comparing the two expressions, we can find:

  • Relative paths are simpler and easier to read
  • Relative paths are more stable, and changes in the structure of the current page can easily cause absolute path changes.

Therefore, relative paths are almost always used for positioning in projects.

xpath basic expression

Expression Explanation Example Example Description
nodename Select all child nodes of this node html Select all child nodes of the root node
/ Select from the root node, or use it to select children Node /html/body/textarea Select all