I have XML like this:
<AAA> <BBB aaa="111" bbb="222"> <CCC/> <CCC xxx="555" yyy="666" zzz="777"/> </BBB> <BBB aaa="999"> <CCC xxx="qq"/> <DDD xxx="ww"/> <EEE xxx="oo"/> </BBB> <BBB> <DDD xxx="oo"/> </BBB> </AAA>
I want to get first <CCC>
element. But with XPath expression //*/CCC[1]
I have got two <CCC>
elements. Each of them is the first elemet in <BBB></BBB>
context. How to get first element in subset?
Find the first element by CSS selector. Find the first element by tag name. Find the first element by partial link text. In order to get the first element of an ID or name of an element, we can use the XPath to display the first value of an element.
The key part of this XPath is *[1] , which will select the node value of the first child of Department .
//div[@class='content'][2] means: Select all elements called div from anywhere in the document, but only the ones that have a class attribute whose value is equal to "content". Of those selected nodes, only keep those which are the second div[@class = 'content'] element of their parent.
XPath text() function is a built-in function of the Selenium web driver that locates items based on their text. It aids in the identification of certain text elements as well as the location of those components within a set of text nodes. The elements that need to be found should be in string format.
This one should work for you:
(//*/CCC)[1]
I want to get first element. But with XPath expression
//*/CCC[1]
I have got two elements. Each of them is the first elemet in<BBB></BBB>
context. How to get first element in subset?
This is a FAQ:
The []
operator has a higher precedence (binds stronger) than the //
abbreviation.
Use:
(//CCC)[1]
This selects the first (in document order) CCC
element in the XML document.
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