...
<element>
<e:Element1 xmlns:e="mynamespace" > ... </.. >
<e:Element1 xmlns:e="mynamespace" > ... </.. >
<e:Element1 xmlns:e="mynamespace" > ... </.. >
<a/>
</element>
...
and this XPath:
//*[local-name()='element']/count(*)
return 4 what is OK.
but now I wanna know count of element1 what is 3. I try a lot of possibilities but with no succes. I have to use local-name and namespace-uri()
int product_count = driver. findElements(By. xpath("//id('product')/x:tbody/x:tr[1]")). size();
XSLT/XPath Reference: XSLT elements, EXSLT functions, XPath functions, XPath axes. The count function counts the number of nodes in a node-set and returns an integer.
To find the XPath for a particular element on a page:Right-click the element in the page and click on Inspect. Right click on the element in the Elements Tab. Click on copy XPath.
Xpath is one the locators used in Selenium to identify elements uniquely on a web page. It traverses the DOM to reach the desired element having a particular attribute with/without tagname. The xpath can represented by the ways listed below − //tagname[@attribute='value'] //*[@attribute='value']
You can try the following:
count(//element/Element1[namespace-uri()='mynamespace'])
If you are using XPath from an environment such as Java or C#, you should first bind a prefix to the namespace, which depends on the API you are using, but will be something like
xpath.declareNamespace("f", "mynamespace")
and then evaluate the XPath expression
count(element/f:Element1)
I deliberately chose a different prefix from the one in your source document just to show that you can use any prefix you like, but of course your code is more readable if you are consistent in your choice of prefixes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With