Summary of common element positioning methods in Selenium

Table of contents

1. Webdriver positioning method

1. Basic positioning usage of xpath

2. xpath relative path/absolute path positioning

3. xpath text, fuzzy, logical positioning


1. Webdriver positioning method

1. ID positioning

driver.find_element_by_id()

2. name positioning

driver.find_element_by_name()

3. Class positioning

driver.find_element_by_class_name()

4. Link positioning --> precise positioning

driver.find_element_by_link_text()

5. partial_link positioning --> fuzzy positioning

driver.find_element_by_partial_link_text()

6. tag_name positioning --> tag name positioning

Find all tags named input on the page and return a list

driver.find_elements_by_tag_name()

7. css selector

driver.find_element_by_css_selector()

8. xpath positioning

// 2 slashes represent relative paths * represent any tags     

driver.find_element_by_xpath()

9. By method

from selenium.webdriver.common.by import By

1)driver.find_element(By.XXX,’定位值’)

 XXX is the name of the above eight positioning methods. The element value of this method can be defined in tuple format.

Suitable for batch management of page elements.

2)driver.find_elements(By.XXX,’定位值’)

This method will locate all elements that meet the conditions and return them in list format

  • Detailed explanation of xpath positioning method

1. Basic positioning usage of xpath

1)  Positioning using attribute values 

driver.find_element_by_xpath(‘//标签名[@属性名=“属性值”]’)

In principle, any value can be used as long as it is a unique value

Eg:-- driver.find_element_by_xpath('//input[@id="loginUsername"]')

-- driver.find_element_by_xpath('//input[@class="form-control tecon required"]')

-- driver.find_element_by_xpath('//input[@placeholder="[email protected]"]')

2) Positioning using multiple attribute values

-Different attribute values ​​can be connected with logical operators, such as and, or

--driver.find_element_by_xpath(‘//标签名[@属性名1=“属性值1” and @属性名2=“属性值2”]’)

-You can also fill it out separately

--driver.find_element_by_xpath(‘//标签名[@属性名1=“属性值1”][ @属性名2=“属性值2”]’)
Eg:--driver.find_element_by_xpath('//input[@id="loginUsername" and @name="username"]')

--driver.find_element_by_xpath('//input[@id="loginUsername"][@name="username"]')

2. xpath relative path/absolute path positioning

1) Absolute path

Starting with /, locate one level at a time starting from the root directory based on the tag name, such as locating the ID input box.

/html/body/div[2]/div/div/from/div[3]/input

2) Relative path

Starting with //, it can be used when the element to be positioned does not have a unique attribute value, but its parent, child, and sibling elements have unique attribute values.

For example, if you want to locate the parent element of the ID input box, but its class attribute is the same as the parent element of the password input box, directly using the class attribute to locate it is not a unique value. You can first position the ID input box element to reversely position its parent element.

//input[@id="loginUsername"]/..

Returning to the upper-level directory is represented by .., which is the same as the Linux operation.

3. xpath text, fuzzy, logical positioning

1) Text positioning

Elements that contain text and will basically not change can be positioned directly using text.

For example, the login button positioning value can be expressed as

//button[text()=”Login”]

 

2) Fuzzy positioning

It can be used when encountering dynamic attribute values, based on the three functions contains(), starts-with(), and ends-with()

1)) contains() contains the attribute value of xxx

//button[contains(@class,"btn")]

2)) starts-with() attribute value starting with xxx

//button[starts-with(@class,"btn")]

3)) ends-with() attribute value ending with xxx

//input[ends-with(@class,"btn-box")]

Guess you like

Origin blog.csdn.net/q_syh/article/details/131540043