Count Elements in Selenium: Complete Guide

The number of elements can be obtained using Selenium by the following methods:

  1. locate items
  2. the numerical value of a figure is known as its length
  3. locate elements
elements = driver.find_elements_by_xpath("//元素路径")
count = len(elements)
print(count)
  1. locate the element
  2. discover items
  3. lend
parent_element = driver.find_element_by_xpath("//父节点路径")
elements = parent_element.find_elements_by_xpath("//元素路径")
count = len(elements)
print(count)
  1. locate elements by their xpath
  2. “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.

bannerAds