Given this XML:
<DocText> <WithQuads> <Page pageNumber="3"> <Word> July <Quad> <P1 X="84" Y="711.25" /> <P2 X="102.062" Y="711.25" /> <P3 X="102.062" Y="723.658" /> <P4 X="84.0" Y="723.658" /> </Quad> </Word> <Word> </Word> <Word> 30, <Quad> <P1 X="104.812" Y="711.25" /> <P2 X="118.562" Y="711.25" /> <P3 X="118.562" Y="723.658" /> <P4 X="104.812" Y="723.658" /> </Quad> </Word> </Page> </WithQuads>
I'd like to find the nodes that have text of 'July' and a Quad/P1/X attribute Greater than 90. Thus, in this case, it should not return any matches. However, if I use GT (>) or LT (<), I get a match on the first Word element. If I use eq (=), I get no match.
So:
//Word[text()='July' and //P1[@X < 90]]
will return true, as will
//Word[text()='July' and //P1[@X > 90]]
How do I constrain this properly on the P1@X attribute?
In addition, imagine I have multiple Page elements, for different page numbers. How would I additionally constrain the above search to find Nodes with text()='July', P1@X < 90
, and Page@pageNumber=3
?
XPath assertion uses XPath expression to select the target node and its values. It compares the result of an XPath expression to an expected value. XPath is an XML query language for selecting nodes from an XML. Step 1 − After clicking Add Assertion, select Assertion Category – Property Content.
Do note that /html/text() doesn't select all text nodes in the document -- only the text nodes that are children (not descendents) of the top, html element. You probably want /html//text() . Some knowledge and understanding of XPath is typically required in order to construct XPath expressions.
5) XPath Text() FunctionThe XPath text() function is a built-in function of selenium webdriver which is used to locate elements based on text of a web element. It helps to find the exact text elements and it locates the elements within the set of text nodes. The elements to be located should be in string form.
Generally I would consider the use of an unprefixed // as a bad smell in an XPath.
Try this:-
/DocText/WithQuads/Page/Word[text()='July' and Quad/P1/@X > 90]
Your problem is that you use the //P1[@X < 90]
which starts back at the beginning of the document and starts hunting any P1
hence it will always be true. Similarly //P1[@X > 90]
is always true.
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