RXpath: Find most recent ancestor

Technology specific object identification, supported applications, web technologies, and 3rd party controls.
mrt
Posts: 236
Joined: Mon Mar 16, 2020 11:31 am

RXpath: Find most recent ancestor

Post by mrt » Thu Sep 15, 2022 12:29 pm

Dear all,

I wonder, how to find out the most recent ancestor of an element.

There are several <h3> tags nested all over the page in different hierarchy levels, and I need to find the ancestor for all of them.
What all of their ancestors share is the first part of the tagname.

So, like this:

Code: Select all

<tag>
  <tag>
    <PARTTAG...>
      <tag>
        <h3>
<tag>
  <PARTTAG>
    <tag>
      <tag>
        <PARTTAG...>
          <tag>
            <tag>
              <h3>
and so on...

So my approach is to find the <h3>, then get the ancestor that starts with "PARTTAG" and move on into that.
With something like this:

Code: Select all

/?/?/?/tag[@tagname>'PARTTAG']//h3[@innertext~$myText]/ancestor::tag[@tagname>'PARTTAG']
But this finds ALL ancestors, meaning for the exact same <h3> element multiple PARTTAGs are found, which makes recognition slow and unprecise.

What I want, is to only find the first recent ancestor.
I tried like this:

Code: Select all

/?/?/?/tag[@tagname>'PARTTAG']//h3[@innertext~$myText]/ancestor::tag[@tagname>'PARTTAG'][][1]
because I was assuming that this works this way (ancestors are an array, and use the first of the array)
but this seems the find the very first topmost PARTTAG, so it looks like the ancestors array is a flat array and it is just picking the first item out of the array, but I would need the first relative to my h3 tag.

EDIT:
Easy to reproduce:

Code: Select all

/dom[@domain='www.ranorex.com']//h3
gives 6 results
-> I would want the 6 most recent ancestor divs where class contains 'blurb' for these h3s

Code: Select all

/dom[@domain='www.ranorex.com']//h3/ancestor::div[@class~'blurb']
gives 12 results

Code: Select all

/dom[@domain='www.ranorex.com']//h3/ancestor::div[@class~'blurb'][][1]
gives 1 result

Code: Select all

/dom[@domain='www.ranorex.com']//h3/ancestor::div[@class~'blurb'][1][]
gives 12 results

Any ideas?

thanks!

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7469
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: RXpath: Find most recent ancestor

Post by odklizec » Mon Sep 19, 2022 12:11 pm

Hi,

It would be much easier to suggest something reliable if you post a Ranorex snapshot.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration