Selenium - How to use Xpath

Since the latest version of Firefox no longer supports development tools such as FireBug, you can add extensions such as Firebug by downloading Firefox versions below 49 from https://ftp.mozilla.org/pub/firefox/releases/  .

What is XPath?

XPath is the path language of XML. In layman's terms, it is to find the tag element through the path of the element.

tool

Xpath can download the Firefox browser plug-in FireBug

 

usage:

1. / (absolute path, selected from the root node) 
2. // (relative path, all child nodes, regardless of whether they are direct children or not) 
3. @ selects attributes 
4. "." selects the current node 
5. ".. "Select the parent node of the current node

Instructions:

Note: //* Represents all elements under the positioning page, the fixed mode @ refers to a certain attribute id = 'xxx'

1. Xpath supports ID, Class, and Name positioning functions. If you transpose * to any tag name, you can filter according to the tag.

Locate by ID
 
//*[@id='i1']
//div[@id='i2']
 
Locate by Class
 
//*[@class='inner']
//div[@class='inner1']
 
Locate by Name
 
//*[@name='name']
//input[@name='name']
class contains div with xxx
//div[contains(@class,’xxx’)]

2. If the tag does not have the three total attributes of ID, Class and Name, Xpath also supports the attribute positioning function

//*[@placeholder= 'Please enter username ' ]

3. Xpath provides hierarchical filtering when tabs are duplicated

Support to progress through / through the hierarchy to find the tags that conform to the hierarchical relationship 
//input[@value=text][8]  ->>第8个input的value=text

//div[@class='inner'][3]/input[@type='text']
When the levels are repeated, they can be positioned through the properties of a single level 

//div[@
class = ' driver ' ][1]/div[@ class = ' inner ' ][3]/input

4. An element whose sibling elements are the same as its label cannot be located through the hierarchy at this time. Because they are all born to one father, they are twin brothers. Xpath provides index filtering

Through the index, the attribute is located in the List, which is somewhat different from the index of python, Xpath starts from 1
 
//select[@name='city'][1]/option[1]

5. If the above concentration is repeated after use, we can use the ultimate artifact provided by Xpath, logical operation positioning. and or

Use and to narrow the scope of the filter, only when the conditions are met can the target be located
 
//select[@name='city' and @size='4' and @multiple="multiple"]
 
or is the opposite, as long as one of these filters appears for so long, it matches
 
//select[@name='city' or @size='4']

 

Xpath extension

following-sibling: Select all sibling nodes after the current node, then if the "sibling" keyword is not added, all nodes above/below are searched, ignoring the concept of siblings, for example:

<div>
<input id="123">
<input>    
</div>

 

To locate the second input: //input[@id='123']/following-sibling::input

  • preceding-sibling: select all sibling nodes before the current node

  • starts-with: starts with so-and-so, for example: //input[starts-with(@class,'xxx')]

  • Absolute path html/body/div/span[2]/input[2] The intermediate structure changes, it will be invalid

  • Relative path //Start, look in the entire html source, no matter where it is

  • Index[x] //div/input[2] The second input below the div

  • Exact match xpath=//*[text()=”Join”]

  • The not keyword means negation, 
    such as finding an input whose id is not 123: input[not[id='123']] 
    , or finding a span whose text does not contain the xxx field: //span[not(contains(text (),'xxx'))]

  • Wildcard * 
    such as //span[@*=”xxx”] specifies that any attribute in the span contains xxx, 
    such as // [@ =”xxx”] specifies that any attribute in the bit page protects the label of xxx

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324933596&siteId=291194637