Reputation: 369
Using findElements()
and size()
methods we can get the count.
But I want to extract the count using count() function in the xpath.
Will the count function return an integer value?
Suppose,
My xpath is (//input[@id='stack'])[3]
and there are 3 matching nodes with //input[@name='hai']
Can I modify my xpath like below?
(//input[@id='stack'])[count(//input[@name=''hai])]
Upvotes: 3
Views: 13664
Reputation: 1412
driver.findElements()
returns a List of WebElements, and .size()
returns the integer size of a list, so I think youd be better off doing the following:
int myCount = driver.findElements(By.xpath("<Your Xpath Here>")).size();
This will get you the count of elements on your page that match your xpath input
Upvotes: 2
Reputation: 111501
Yes, if
count(//input[@name='hai'])
evaluates to
3
then
(//input[@id='stack'])[count(//input[@name='hai'])]
will select the same nodes as
(//input[@id='stack'])[3]
would select.
However, your intent is quite unclear, especially given that
//input[@id='stack']
will select all of the input
elements with
an id
attribute value of 'stack'
. Usually id
attribute values
are unique across the document, so usually this would select only a
single element.
(//input[@id='stack'])[count(//input[@name='hai'])]
assumes that there are at least as many input
elements id'ed as 'stack
' as there are input
elements named 'hai'
-- an odd assumption.
Upvotes: 5