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.
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.
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.
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.
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.
count(...)
value. Edited but kept the answer for reference. –
Contingence 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()
.
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); }
© 2022 - 2024 — McMap. All rights reserved.