No longer afraid of Web automation test element positioning, Python+Selenium teaches you to quickly locate Web elements

 B station first push! The most detailed collection of automated tests in 2023 can be mastered by novices, making testing simple, fast and reliable icon-default.png?t=N3I4https://www.bilibili.com/video/BV1ua4y1V7Db

 

Table of contents

1. Locate elements based on ID

2. Locating elements by name

3. Locate elements based on class names

4. Locate elements based on tag names

5. Locate elements based on link text

6. Locate elements based on partial link text

7. Locating elements based on CSS selectors

8. Locating elements based on XPath

1. Locate multiple elements based on the class name

2. Locate multiple elements according to the tag name

3. Locate multiple elements based on CSS selectors

4. Locate multiple elements based on XPath


 In web automation testing, element positioning is a very critical step. We need to perform subsequent operations by positioning elements, such as entering text, clicking buttons, and so on. The following are some common element positioning methods and their corresponding code implementations:

1. Locate elements based on ID

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.example.com')
element = driver.find_element_by_id('element_id')

2. Locating elements by name

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.example.com')
element = driver.find_element_by_name('element_name')

3. Locate elements based on class names

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.example.com')
element = driver.find_element_by_class_name('element_class')

4. Locate elements based on tag names

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.example.com')
element = driver.find_element_by_tag_name('element_tag')

5. Locate elements based on link text

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.example.com')
element = driver.find_element_by_link_text('link_text')

6. Locate elements based on partial link text

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.example.com')
element = driver.find_element_by_partial_link_text('partial_link_text')

7. Locating elements based on CSS selectors

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.example.com')
element = driver.find_element_by_css_selector('#element_id')

8. Locating elements based on XPath

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.example.com')
element = driver.find_element_by_xpath('//input[@id="element_id"]')

The above are several commonly used element positioning methods, and select the appropriate method for positioning according to the actual situation.

Instead of targeting a single element, we can also target multiple elements. The following are several common methods of locating multiple elements and their corresponding code implementations:

1. Locate multiple elements based on the class name

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.example.com')
elements = driver.find_elements_by_class_name('element_class')

2. Locate multiple elements according to the tag name

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.example.com')
elements = driver.find_elements_by_tag_name('element_tag')

3. Locate multiple elements based on CSS selectors

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.example.com')
elements = driver.find_elements_by_css_selector('.element_class')

4. Locate multiple elements based on XPath

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.example.com')
elements = driver.find_elements_by_xpath('//input')

The above are several commonly used methods for positioning multiple elements, and it is also necessary to choose an appropriate method for positioning according to the actual situation.

It should be noted that when using these positioning methods, if the corresponding element cannot be found, a NoSuchElementException will be thrown. Therefore, we need to use try-except statement to handle this situation to avoid program crash.

 Structural diagram of automated test learning steps:

Automated welfare testing:


[Test exchange group number]: 574737577 icon-default.png?t=N3I4http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=mQ7BUegSKrQqXAznDodcBM2ejt9jh22V&authKey=pKW6wUN%2FVg35%2BqaxnE7NJ3I8H8UhaT%2FNBdK1Y zcsNGQ3gXBdHrFFNOw2CSsXMREb&noverify=0&group_code=574737577

Guess you like

Origin blog.csdn.net/Free355/article/details/130472622