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 |
// | Select starting from the matching node, or select descendant nodes | /html/body//textarea | Select all in |
| | Select multiple nodes | //div|//a | Select all
nodes and nodes |
… | Select the parent node of the current node | //p/… | Select all
The parent node of |
* | Select all nodes | /html/body/* | Select all nodes in |
xpath predicate expression
Predicate expression (predicate): An expression immediately following the node and embedded in [], which can be used to filter multiple nodes with the same name.
The working principle of predicate expression: obtain node information, judge whether the node meets the requirements through the expression, and return the node if it is True
Predicate format:
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-javascript">Node name<span style=\ "color:#cccccc">[</span>Predicate<span style="color:#cccccc">]</span> </code></span></span>
Common predicates | Explanation | Example | Example Description |
---|---|---|---|
n | Node number | //div[2] | Select all
nodes that are the second
node of their parent |
last() | Last node | //div[last()] | Select all
nodes that are the last child node of their parent |
position() | Compare node numbers | //div[position() < 3] | Select all
nodes that are the first two
nodes of which this node is a parent |
@attribute | attribute | //input[@id] | Select all nodes with id attributes |
@attribute=’attribute value’ | Compare attributes | //input[@id=’kw’] | Select all nodes And the node id attribute is ‘kw’ |
text () | Compare node text | //a[text()=’News’] | Select all nodes whose text is ‘News’ |
Predicate operator | Explanation | Example | Example description |
---|---|---|---|
= != > < >= <= |
Size comparison | /html/body/div[position()=2] | Select the second
child node in |
+ – *, div mod |
Addition, subtraction, multiplication and division find remainder | /html/body/div[last()-1] | Select the second to last
child node in |
and or not() | OR, AND, NOT operations | //*[@id=’kw’ and @class=’s_ipt’] | Select all nodes with id=’kw’ and class=’s_ipt’ |
contains() | contains | //*[contains(text(), ‘News’)] | Select all nodes with the word ‘news’ in their text |
starts-with() | Header contains | //*[starts-with(@id, ‘s’)] | Select all nodes whose text begins with ‘news’ |
Remark:
- last(), position(), text(), not() have brackets The expressions are essentially functions provided by xpath. This article only lists common functions.
- According to the W3C standard, the first node number should be [1], but in IE5 and above the first node number is [0 ]
xpath axis (axis)
Axis: represents the tree relationship between the selected node and the current node, used to filter a type of nodes that have the same relationship with the current node
Axis format:
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-javascript">Current node<span style=\ "color:#67cdcc">/</span>Axis name<span style="color:#67cdcc">:</span><span style="color:#67cdcc">:</span >Node name </code></span></span>
axis | Explanation | Example | Example explanation |
---|---|---|---|
ancestor | Ancestor node | //input[@id=’kw’]/ancestor::\ * | Select all ancestor nodes of the node with id=’kw’ |
ancestor-or-self | Ancestor node and current node | Omitted | Omitted |
attribute | Node attributes | Omitted | omitted |
child | Child node, equivalent to / | /html/body/child::div | Select all
child nodes in |
descendant | Descendant nodes, equivalent to // | /html/body/descendant::div | Select all
descendant nodes in |
descendant-or-self | Descendant nodes and current node | Omitted | omitted |
following | All nodes after the end tag | omitted | Omit |
namespace | namespace | omitted | Slightly |
parent | Parent node | Omitted | Omitted |
preceding | All nodes before the start label | Omitted | Omitted |
preceding-sibling | All sibling nodes before the current node | Omitted | Omitted |
self | Current node | Omitted |
slightly |
Finally, I would like to thank everyone who read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can just take it away:
This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 139272 people are learning the system