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

5

9

How to count the number of elements are matching with for the given xpath expression

xpath: driver.findElement(By.xpath("//div[contains(@id,'richedittext_instance')]"))

all i need is the count.

Cowrie answered 8/2, 2013 at 6:17 Comment(0)
O
14

Try this code:

//Assume driver is intialized properly.
int iCount = 0;
iCount = driver.findElements(By.xpath("Xpath Value")).size());

The iCount has the number of elements having the same xpath value.

Obscure answered 8/2, 2013 at 7:5 Comment(0)
P
1

Another option If you are basing your requirements strictly on the need to use Selenium, you might be able to do something like this using WebElements and getting the size of the returned list:

List<WebElement> myListToCheck=currentDriver.findElements(By.xpath("somePath"));
if(myListToCheck.size()>0){
//do this
}else{
//do something else
}

Or just simply returning the size of the returned list; if that's all you really want to get from it...

int mySize=myListToCheck.size()

I believe once you have an established WebElements list, you can also use iterators to go over that list. Helpful, I dunno... just providing another way to get to the same end-game.

Phidias answered 29/6, 2016 at 18:49 Comment(0)
C
0

Not working in Selenium, which only allows to return nodes from XPath, not primitives like the number returned by count(...). Kept for reference and is valid for most other tools offering a more complete XPath API.

You should only return least possible amount of data from the query. count(//div[contains(@id,'richedittext_instance')]) counts the number of results within XPath and thus is faster as all the elements do not have to be passed from the XPath engine to Selenium.

I can't help you with how to fetch this as n int out of selenium, but this should be easy stuff.

Contingence answered 8/2, 2013 at 13:19 Comment(4)
I don't think this works as I have been trying to use this already unsuccessfully.Poona
Since then I learned Selenium is not able to return the count(...) value. Edited but kept the answer for reference.Contingence
When you say "Other tools" what tools are you referring to? I ask as over the past 2 years I have been developing a browser game bot and have used so many different tools non of which managed to "mimic" a human the way Selenium Webdriver can.Poona
There are several dozens of XPath processors. But I do not know about another one with similar features like Selenium.Contingence
G
0

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

Gal answered 13/12, 2020 at 1:15 Comment(0)
C
0

I wanted to print the number of elements that share the same xpath. Using the below code - I am able to print the number of elements in both console and extent report.

enter code here

List<WebElement> noOfTiles = baseTestSuite.getDriver() .findElements(By.xpath("//android.widget.RelativeLayout[contains(@resource-id,'pf_tile_outer')]")); if(noOfTiles.size()>0){ System.out.println("No. of tiles on Add cash page are:"+" "+noOfTiles.size()); int numberOfTiles = noOfTiles.size(); // converting the int to String because getLogger is not to print the integer. String s=String.valueOf(numberOfTiles); getLogger().log(LogStatus.PASS, s); }

Cure answered 11/12, 2023 at 8:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.