6

I looked at the documentation located here, but couldn't find an answer.

I want to get an element by class name or xpath and return the number of instances. There seems to be no available function in Python, such as get_xpath_count().

Any ideas on how to achieve this?

7 Answers 7

12

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

Sign up to request clarification or add additional context in comments.

3 Comments

doh! good catch! I was looking at the singular find_element_by_xpath() which was listed in the Locating Elements section and it only returned a single WebElement object. The plural function you stated was actually located in section 6 "API". Not sure why they didn't include this in section 4 as well. Thanks!
Yeah it would make sense at least making a brief mention in section 4, strange but at least there are some docs!
I'm gonna edit the doc on github and hopefully it gets pulled.
10

You Can simply use len() function:

len(driver.find_elements_by_xpath('//a'))

Comments

3

In java, the following could 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);

For counting the total links in a page:
Way1:

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

Way 2:

int totalLinkSize2 = driver.findElements(By.xpath("//a")).size();
System.out.println("Total Links by Way2 : " + totalLinkSize2);

5 Comments

is this the best way in java?
The above is one of the ways. You can do it by different way also. You can use List<WebElement>
okay, that is what i am doing right now, but is there another way like a method that will return an int object?
You can write a method yourself by using the above code.
this isn't a valid answer. the OP tagged the question as Python, not Java
1

In python

element.find_elements()

will return all surface children Web Elements

Comments

1

use this :

count_elemnts = len(driver.find_elements_by_...('name or class or id or xpath'))

elements (with an s) and not element

1 Comment

Just adding a brief explanation, for others. Elements (with an s) is used here over just the non-pluralised Element because it iterates over the whole list, instead of just one element. See here for more info: stackoverflow.com/a/39520898/20331138 The len() function is also implemented as it helps to calculate the length of the list object, or the number of instances of elements in the list i.e. the count of data items present in the list.
0
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;

Comments

-1

You can use 'assertXpathCount' command available in Selenium

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.