How to get the number of items found using Selenium WebDriver with Python?

I looked at the documentation located here , but could not find the answer.

I want to get a class name or xpath element and return the number of instances. There seems to be no function in Python, like get_xpath_count() .

Any ideas on how to achieve this?

+4
source share
6 answers

Try driver.find_elements_by_xpath and count the number of items returned.

+7
source

You can simply use the len() function:

 len(driver.find_elements_by_xpath('//a')) 
+3
source

In java, the following may work:

 int xpathCount= driver.findElements(By.xpath("//div[@id='billingProfiles']/div[@class='cardContainer']")).size(); 

OR

 List<WebElement> xpath = driver.findElements(By.xpath("//div[@id='billingProfiles']/div[@class='cardContainer']")); int xpathCount = xpath.size(); System.out.println("Total xpath: " + xpathCount); 

To count common links on a page:
Way1:

 List<WebElement> totalLinks = driver.findElements(By.tagName("a")); int totalLinkSize = totalLinks.size(); System.out.println("Total Links by Way1 : " + totalLinkSize); 

Method 2:

 int totalLinkSize2 = driver.findElements(By.xpath("//a")).size(); System.out.println("Total Links by Way2 : " + totalLinkSize2); 
+1
source
 public static IWebDriver driver = null; public static IList<IWebElement> elements; // List count return total number of element elements = driver.FindElements(By.XPath("//a")); int intLinkCount = elements.Count; 
0
source

You can use the "assertXpathCount" command available in Selenium

0
source

In python

 element.find_elements() 

will return all web elements of child elements

0
source

Source: https://habr.com/ru/post/1386315/


All Articles