How to use xPath to identify elements to the right of text

Hey guys,
I been struggling to figure this out… it seems like Katalon object spy doesn’t capture xpath:neighbor attribute if an element is on right side of a neighboring element.

This is probably easy but i’m just not getting it. I’m trying to have the test select checkbox by using the neighbor text attributes: “include cashFlow” and “Select All”
but the object spy doesn’t have the xpath:neighbor option… but if i spy on “Includes cashFlow” - it does.

image

If you can show the HTML of the two checkboxes, we may be able to show various xpaths that can allow you to do so.

There’s no unique identifier for the checkbox… besides [div] and alt# - which is not ideal as these checkbox [#s] changes with various settings. I just need the test to select checkbox following/preceeding those texts (whichever would work)

If the xpath:img reference is to the Include Cashflow checkbox, then you should be able to adjust the 8 to a 9 to refer to the “Select All” checkbox. Perhaps use the xpath:position reference as //div[9]/div/div[2]/div/img/following-sibling::img
(since the two <img> are on the same level I use following-sibling ).

yea that would have been easy if these div were always static. But depends on the settings on the page, the divs #s would change, so then the test would likely look at wrong divs…
i’ve been having those issues with test failing so though the best way to get this right is to let test know to select the checkbox next to “Include Cashflow” … i find it weird that there’s xpath:neighbor attribute if we capture “Include CashFlow”, but i don’t think we ever get any xpath:neighbor if the element is on the right side.

@ashraf.madina

Exactly which HTML element do you want to select?

If you want the <img> element next to the <span>Include Cashflow</span>, then the following XPath expression will do:

(//span[contains(.,'Include Cashflow')]/following-sibling::img)[1]

Yes! This will do! thx

In the above XPath expression I used several "know how"s. I would list the pointers for references.

contains() function

Abbreviated Syntax .

. stands for the context node

https://www.w3.org/TR/1999/PR-xpath-19991008#path-abbrev

In this case, . is equal to the text() of <span> element

text() function

following-sibling axis

(//span[contains(.,'Include Cashflow')]/following-sibling::img)[1]

This XPath expression has the following outer-most structure.

( Expr )[ PredicateExpr ]

Expr here is

//span[contains(.,'Include Cashflow')]/following-sibling::img

this returns a NodeSet which comprises with 2 <img> element(s) in the following-sibling position of the <span> element(s) with of which content text contains “Include Cashflow”.

[1] after () is a filter expression that filters out the first node of the given NodeSet. In the above example we have 2 <img> nodes. Out of them we get the first <img> node.

So i’m in a similar situation and the same logic seems to not work?
I’m trying to check/uncheck the box after the Net Worth. I don’t know why katakin doesn’t provide an xPath if the element you want is on the right of something… but always provides if something you select is on the left.

Example: If i want to select the Net Worth - spy web will produce //*/text()[normalize-space(.)='Net Worth']/parent::*

But If i want to select the checkbox next to the Net Worth - it never gives the xpath for it.

I tried the above xpath code but doesn’t recognize it. Any idea?

Can you show the HTML of the area so we can assist you?

I think i was able to solve it…but let me see what you got.

You can try the following:

xpath:  '//span[text()="Net Worth"]/following-sibling::span[1]/img'
XPath Explaination

Start at the span tag that has the text, Net Worth, and then drop down to the first sibling span (another tag at the same level) and then downward to the img tag.

Just a note that if you want to move up to another sibling, then use preceding-sibling.