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.
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
Try to iterate the elements with Get Web Elements
and Get Web Element
.
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
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).
Regards
Charlie
2 Likes
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
3 Likes