Discussion on Xpath Positioning Method

Commonly used Xpath positioning methods and characteristics

a. Use absolute paths to locate elements

E.g:

1 driver.find_element_by_xpath ("/html/body/div/form/input"

 

This path is the path from the actual tag <html> of the web page to the element to be positioned. But if there is any increase or decrease in the element between the element to be positioned and the element at the beginning of the page, the element positioning will fail

b. Use relative paths to locate elements

E.g:

1 driver.find_element_by_xpath ("//input")  

 

  Returns the first matching element found

Relative paths generally only contain elements related to the nearest layers of the positioned element. If the relative path is well written, the impact of page changes will be minimal and the positioning will be accurate.

c. Use the index to locate the element, the initial value of the index is 1

E.g:

1 driver.find_element_by_xpath ("//input[2]") 

 

  Returns the second found element that matches the condition

If there are multiple similar elements in a page, or there are multiple identical elements under a layer, you need to use the index method to locate, otherwise it will be impossible to distinguish

d. Combine attribute values ​​to locate elements

E.g:

1 driver. find_element_by_xpath ("//input[@id='username']")

 

Attribute positioning is also a common method. If there are no common attributes such as id, name, class, etc. that can be called directly in the element, you can also find out whether there are other attributes in the element that can uniquely identify the element. If so, you can use this method positioning

e. Use logical operators to locate elements in combination with attribute values, and and or

E.g:

1 driver. find_element_by_xpath ("//input[@id='username' and@name='userID']")

 

The joint positioning of multiple attribute values ​​can more accurately locate the element. And if there are multiple elements with the same label, if they contain different attribute values, this method can also be used to distinguish

f. Use attribute names to locate elements

E.g:

1 driver. find_element_by_xpath ("//input[@button]")

 

This method can distinguish elements of the same tag with different attribute names. Positioning is relatively simple, but there are also multiple elements that cannot be distinguished from the same type of label with the same attribute name. At this time, it is necessary to coordinate with index positioning.

g. Match elements using partial attribute values

E.g:

1 (a)starts-with()
2 driver. find_element_by_xpath ("//input[stars-with(@id,'user')]")
3 (b)ends-with()
4 driver. find_element_by_xpath ("//input[ends-with(@id,'name')]")
5 (c)contains()
6 例如:driver. find_element_by_xpath
7 ("//input[contains(@id,"ernam")]")

This method is more flexible, and can locate the case where the attribute value is not regular, or partially changed, and there are spaces in the middle. Note: If the attribute value contains spaces, Webdriver is prone to errors when locating, sometimes it can be located and sometimes it cannot be located, so you should avoid using attribute values ​​with spaces to locate. This method can be used for partial attribute value positioning

h. Match elements with arbitrary attribute values

E.g:

1 driver. find_element_by_xpath ("//input[@*='username']")

This method is equivalent to a fuzzy query, as long as the tag to be located, such as any attribute value in the input is equal to 'username', the match can be successful. The disadvantage is that it may match other elements containing this attribute value, so we need to check whether this element value is unique in the page when positioning

The idea of ​​using Xpath to locate elements

a. First see if the element has an obvious, unique attribute value. If there is, use the relative path plus the attribute value to locate

b. If the element to be located does not meet the above characteristics, the attribute of the element is either dynamic, or the element cannot be distinguished, or there is a space in the attribute value. Starting from this element, look up the level above it.

c. When a matching element is encountered, write xpath to it, and then verify whether it can be located in selenium IDE

d. Then start from this element, and write down one level, until the element to be positioned

e. When selenium IDE verifies that the positioning is successful, put it into the test case to debug and run

Guess you like

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