How to count the number of elements are matching with for the given xpath expression
xpath: driver.findElement(By.xpath(\"//div[contains(@id,\'richedittext_insta
Do the following:
from selenium.webdriver.common.by import By
elements = driver.find_elements(By.XPATH, "Your_XPath")
This outputs a list of selenium.webdriver.firefox.webelement.FirefoxWebElement
s (in my Firefox browser).
Finally, find out the length of the list:
len(elements)
NB.: Please note that I have written find_elements()
(plural) and NOT find_element()
. Both of them are different. find_element()
only returns the first matched web element, but to find the list of all the matched web elements, we have to use find_elements()
.