# Searching element that are at a different xpath level

**URL:** <https://forum.robotframework.org/t/searching-element-that-are-at-a-different-xpath-level/7700>\
**Category:** Robot Framework\
**Created:** [14 August 2024 10:19 UTC](https://forum.robotframework.org/t/searching-element-that-are-at-a-different-xpath-level/7700 "2024-08-14T10:19:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![coopernico46](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@coopernico46](https://forum.robotframework.org/u/coopernico46)\
**Post date:** [14 August 2024 10:19 UTC](https://forum.robotframework.org/t/searching-element-that-are-at-a-different-xpath-level/7700/1 "2024-08-14T10:19:24Z")

</div>

Hi all,

I’m stuck with a script with which I intend to search for a specific property and if this property exist then i have to extract the text from another div that it’s at a different xpath level.

Please see the image attached cause i thought it’s better to understand the code.

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/7/7575f130740217c1622ed2d4d4aea31d8b61e4fa.png)

I have no problem to Get Count Elements with @checked property using (don’t mind the [1]):  
${count} Get Element Count (//div[@id=‘flight-card’])[1]//input[@type=‘radio’ and @checked]  
So i can know if there are 1, 2 or more items with @checked property but the i have problems to interact correctly to get the Text for each of the items with the @checked property (in my code always get the text for the first one @checked item) using something like this:

```
Set Variable ${Con_check} (//div[@id='flight-card'])[1]//input[@type='radio' and 
@checked]
Set Variable ${compañia_name} (//div[@id='flight-card'])[1]//input[@type='radio' and 
@checked]/ancestor::div[contains(@id,'-option') and 
contains(@class,'styles_option__')]//div[@data-qa='company-name']

${count} Get Element Count ${Con_check}
Log ${count}

FOR ${Con_check} IN (//div[@id\='flight-card'])[1]
        ${nombre} Get Text ${compañia_name}
        Log ${nombre}
END

```

---

<div class="post-metadata">

**Author:** ![HelioGuilherme66](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/helioguilherme66/32/34_2.png) [@HelioGuilherme66](https://forum.robotframework.org/u/HelioGuilherme66)\
**Post date:** [14 August 2024 12:19 UTC](https://forum.robotframework.org/t/searching-element-that-are-at-a-different-xpath-level/7700/2 "2024-08-14T12:19:21Z")

</div>

Try to iterate the elements with `Get Web Elements` and `Get Web Element` .

---

<div class="post-metadata">

**Author:** ![coopernico46](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@coopernico46](https://forum.robotframework.org/u/coopernico46)\
**Post date:** [14 August 2024 14:09 UTC](https://forum.robotframework.org/t/searching-element-that-are-at-a-different-xpath-level/7700/3 "2024-08-14T14:09:34Z")

</div>

Hi @HelioGuilherme66  
Thanks for your help.  
I tried this script but looks like the system always retrieve the same Text, i mean the text related to the first item with @checked property:  
Set Variable ${Con\_check} (//div[@id=‘flight-card’])[4]//input[@type=‘radio’ and  
@checked]

```
${Elements_with_checked} Get WebElements ${Con_check}
Log ${Elements_with_checked}

FOR ${element} IN @{Elements_with_checked}
    ${item_checked} Get WebElement ${element}
    Log ${item_checked}
    ${cia_name} Get Text (//div[@id='flight-card'])[4]//input[@type='radio' and 
@checked]/ancestor::div[contains(@id,'-option') and 
contains(@class,'styles_option__')]//div[@data-qa='company-name']
END

```

I don’t know well how to use the webelement into the Get Text so the xpath search for the Text in the second WebElement (sorry i have to say that i get 2 webelements).

Regards

---

<div class="post-metadata">

**Author:** ![CharlieScene](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/charliescene/32/3484_2.png) [@CharlieScene](https://forum.robotframework.org/u/CharlieScene)\
**Post date:** [14 August 2024 14:46 UTC](https://forum.robotframework.org/t/searching-element-that-are-at-a-different-xpath-level/7700/4 "2024-08-14T14:46:45Z")

</div>

Hi,

It seems in your examples above that you fix some indices, and not going through the correct nodes.

First this:

```
Set Variable ${Con_check} (//div[@id='flight-card'])[1]//input[@type='radio' and @checked]

```

Will stop at the first “flight-card” div, then may indeed return several input located below. But it has no text associated below (green area in your screen). The text you need is in a following-sibling of the input’s div parent.

Then in your first Get Element Count and FOR loop, you overwrite ${nombre} at each loop, so you will always get the last, or same name (as previously you fixed to the first). You should either name the variable in a incremental way, or add it to a list.

In your second case, you Get Webelements which returns a list, but then you loop and reevaluate it in the loop, and use Get Text on a fixed variable. The point here is to extract text from ${element}.

To my point of view, you should (to adjust of course), use the correct xpath variable to aim the text in the div (verify in inspector that it highlights the correct ones).  
I would say something like this :

```
     VAR ${Con_check} //div[@id='flight-card']//input[@type='radio' and @checked]/following::div[@data-qa='company-name'][1]//div
     ${Elements_with_checked} Get WebElements ${Con_check}

     VAR ${i} 1
     FOR ${element} IN @{Elements_with_checked}
         ${cia_name${i}} Get Text ${element}
         ${i} Evaluate ${i} + 1
    END

```

==\> Here we search for every flight card id, with an input radio and checked, then the first following div company-name, and finally the div containing the text.  
Then you get webelements and loop on it by taking are of not overwriting the variables (incrementing the name).

 ![2024-08-14_16h34_48](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/5/592916f10e83a47c564297eb2896d583d61264c2.png)

Regards  
Charlie

---

<div class="post-metadata">

**Author:** ![coopernico46](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@coopernico46](https://forum.robotframework.org/u/coopernico46)\
**Post date:** [14 August 2024 15:45 UTC](https://forum.robotframework.org/t/searching-element-that-are-at-a-different-xpath-level/7700/5 "2024-08-14T15:45:28Z")

</div>

Thanks a lot @CharlieScene  
Your code works as expected. The key for me was th ${Con\_check} i was focused in an incorrect xpath so i can’t get the expected text… i tried several times with different code but always keep the same ${Con\_check}. Regarding to the [1] in (//div[@id=‘flight-card’])[1] that’s correct cause i’m only interested in this first one item

My final code following your steps it’s:

```
Set Suite Variable ${Con_check} (//div[@id='flight-card'])[11]//input[@type='radio' and 
@checked]/following::div[@data-qa='company-name'][1]//div
${Elements_with_checked} Get WebElements ${Con_check}
${cia_names} Create List

FOR ${element} IN @{Elements_with_checked}
    ${cia_name} Get Text ${element}
    Append To List ${cia_names} ${cia_name}
END

Log Las compañías son: ${cia_names} console=True
Log Nombre de la primera compañía: ${cia_names[0]} console=True
Log Nombre de la segunda compañía: ${cia_names[1]} console=True

```

As you can see i changed a little bit and finally create a list to save the names.

Thanks a lot
