Table of contents
1. Locate elements based on ID
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: