Selenium2+python automation 7-xpath positioning

foreword    

In the last article, I briefly introduced the use of tools to view the xpath address of the target element. The tool view is relatively rigid and not flexible enough. Sometimes direct copy and paste will not locate it. At this time, you need to manually write xpath yourself. This article explains some of the syntax of xpath in detail. 

What is xpath?

Official introduction: XPath is the XML path language, which is a language used to determine the location of a certain part of a document in XML1 ( a subset of Standard Universal Markup Language 3 ). Anyway, Xiaobian sees that this introduction is cloudy and foggy. In layman's terms, the element is found through the path of the element, which is equivalent to locating the coordinates of an object to find the object.

1. xpath: attribute positioning

  1. xptah can also be located by attributes such as id, name, and class of elements, as shown below

 

    2. So you can use the following xpath method to locate

 

Second, xpath: other attributes

    1. If an element has no id, name, and class attributes, it can also be located through other attributes at this time.


Three, xpath: label

    1. Sometimes the same attribute has more names with the same name. At this time, it can be filtered by the label to make the positioning more accurate.

    2. If you don't want to make a label name, you can use * to indicate any label

    3. If you want to formulate a specific label, you can directly write the label name

 

Four, xpath: level

    1. If an element, its attributes are not very obvious and cannot be located directly, then we can first find its father (parent element)

    2. After finding its father, find the next level to locate it

    3. As shown in the figure above, the label to be positioned is the input, its father's id=s_kw_wrap.

    4. If its father's attributes are not very obvious, find its grandfather id=form

    5. So it can be located through the hierarchical relationship

 

Five, xpath: index

    ​1. If an element has the same label as its siblings, it cannot be located through the hierarchy. Because they are all born to one father, they are twin brothers.

    2. Although it is difficult to identify twin brothers, they are born one after the other, so they can be located by their rank in the family.

    3. The triplet brothers as shown below

 

    ​4. Use xpath to locate the eldest, second and third (the index here starts from 1, which is different from the Python index)

 

Six, xpath: logical operation

    ​1.xpath also has a relatively strong function, which can perform logical operations on multiple attributes, and can support and (and), or (or), and not (not)

    2. The and operation is generally used more, which satisfies two properties at the same time

 

 

Seven, xpath: fuzzy matching

    ​1.xpath also has a very powerful function, fuzzy matching

    2. Master the fuzzy matching function, basically there is nothing that cannot be located

    ​3. For example, I want to locate the hyperlink "hao123" of the Baidu page. As mentioned in the previous article, it can be located through by_link, or through by_partial_link, fuzzy matching. Of course xpath can also have the same function, and more powerful.

 

You can think of xpath as the slaying knife of element positioning. Martial arts supreme, treasure knife xpath, css is out, who will compete with each other? The next lesson will show the css positioning of Yitianjian.

 

Guess you like

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