Count Elements in Selenium: Complete Guide
The number of elements can be obtained using Selenium by the following methods:
- locate items
- the numerical value of a figure is known as its length
- locate elements
elements = driver.find_elements_by_xpath("//元素路径")
count = len(elements)
print(count)
- locate the element
- discover items
- lend
parent_element = driver.find_element_by_xpath("//父节点路径")
elements = parent_element.find_elements_by_xpath("//元素路径")
count = len(elements)
print(count)
- locate elements by their xpath
- “The length”
elements = driver.find_elements_by_xpath("//元素路径")
count = len(elements)
print(count)
You can choose from the three methods based on specific needs. The find_elements_by_xpath method locates elements by their XPath path, but you can also use other methods like find_elements_by_css_selector or find_elements_by_id.