Selenium Xpath and Css positioning

1. Xpath positioning

1.1. Absolute positioning

Absolute path: starting from the root node, find the label you want to locate layer by layer
Expression: generally start with a left slash/start with a left slash/separated
Not recommended, unless you have used all the methods and still cannot locate the
Span label Absolute positioning is usually used.
When it is determined that the xpath path is unique, it is written into the code.
If there are multiple tags in the path of the tag level, then you need to add [] to indicate the number of tags (the following example)
The subscript starts from 1 The
core is how to locate:
1) The sidebar travel navigation in the positioning project

Enter the positioning::/html/body/div/div/ul/li/b
query and find that there are 7 identical controls

Please add image description

2) Modify the absolute path of
positioning Input positioning: /html/body/div/div/ul/li[3]/b
If there is only one control, you can write the code
Note: Add [] from back to front. Basically, start from the back layer by layer, first locate a unique one, and then use the [] serial number to distinguish which
one

code show as below:
Please add image description

1.2. Relative positioning

Relative path: start from a certain level with special attributes in the middle to locate the label you want to locate
Expression: generally start with double left slash ///input[@class="but1"]
starts with //, start from parent with special attributes (id/name/class)

Scenario 1 Unique attribute positioning

Normal case (unique label)
//*[@id="_allSearchKeyword"]
Please add image description
Please add image description

Scenario 2 Multi-attribute positioning

If you cannot locate a unique value through a certain attribute, use and to connect multiple attributes
//input[@name="key" and @type="text" and @placeholder="Please enter the keyword you are looking for"]

Positioning: //input[ @type="text" and @placeholder="airport, train station, hotel name or area"]
Please add image description
Please add image description

Scenario 3 word segmentation positioning

When locating dynamically changing tags, as long as the attribute value I want to locate contains xxx, then it is the tag I want to locate //input[contains(@placeholder, "please input")]
For example: there is an id, which is followed by It is a number that changes dynamically, such as: auto12345, 12345 may change, but auto is what we want, that is, to verify a "contain" relationship, as long as auto is included, it is the control we want
Please add image description

Scenario 4 Text Positioning

To locate the control through text information, : Regular matching, no matter what label has text in the middle // [text()="Home Improvement Festival"]
Please add image description

Scenario 5 The father finds his son to locate

If the control does not have special attributes, you can look for the parent. Action and method, form, type, and style are generally not used. Try to find the id, class, name, placeholder, and text attributes. If the parent still cannot find it, go to parent's parent find
Please add image description

Scene 6 Son looking for father to locate

Multi-level and so on (//input[@class='but1']/…) If
you can't find a chestnut with a special image, just write it.
Please add image description

2. Css positioning

2.1. Absolute positioning

Absolute path: start from the root node, and find the label you want to locate layer by layer.
Expression: generally separated by left slash/beginning with greater than sign>
Please add image description

2.2. Relative positioning

For css_selector, the number of children does not refer to the order of the same label but to the order of all labels under the parent; xpath is the
order

Scenario 1 by id

When there is an id attribute in the tag, it can be written as: tag type #id attribute value (the tag type can be omitted)
Please add image description

Scenario 2 by class

When there is a class attribute in the tag, it can be written as: tag type.class attribute value, such as: input.but1
Please add image description

Scene 3 by tag

Use tag type [attribute value=""] to locate, such as: input[placeholder="Please enter the keyword you are looking for"]
Please add image description

Scenario 4 Multi-attribute

Connect multiple attribute values: input[class="but1"][placeholder="Please enter the keyword you are looking for"
]Please add image description

Scene 5 Father looking for son

Please add image description

:first-child. Positions the first child
:nth-child(n) The Nth child of its parent, regardless of the element type.
n can be a number, keyword or formula

:last-child locates the last child

:nth-last-child(n) locate the nth last child

If there is any infringement, please contact by email, I am sorry.
This is only for learning personal notes, and if there is any reprint, please indicate the source.
Contact email: [email protected] Let’s
learn to test and open the penguin group together (chat, water group, advertising do not disturb): 826471103

Guess you like

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