Count the number of elements are matching with for the given xpath expression

前端 未结 4 1718
自闭症患者
自闭症患者 2021-02-14 20:35

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         


        
4条回答
  •  情深已故
    2021-02-14 21:14

    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.FirefoxWebElements (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().

提交回复
热议问题