What can i use to save elements in a variable?

i’m trying to save all these ‘li’ elements in a variable but im not having luck.

i tried with this but didn’t worked

what i have to do after get those elements is just save only its text.
how can i accomplish that?

Hi @maurosouto27

I have 2 options. The first option create dynamic variables of each element with its class name and value is element text. The second option is to append all element texts into list. Third option would be to create dictionary with class name as key and element text as value.

Example code below

Minimal task
    Open Available Browser    file://${CURDIR}${/}test.html
    ${elements}=    Get WebElements    //li
    ${texts}=    Create List   # List for storing text element texts
    FOR    ${element}    IN    @{elements}
        ${class}=    Get Element Attribute    ${element}    class
        Set Suite Variable    ${li_${class}}    ${element.text}   # Option 1. Create dynamic variable based on element class
        Append To List    ${texts}    ${element.text}   # Option 2. Append element text into list
    Log to Console    AB: ${li_ab}   # Log one variable created by option 1
    Log to Console    ${texts}   # Log list containing texts from all elements
    Log    Done.
1 Like

my problem is the line “${elements}= Get WebElements //*[@id=“formEmision”]/div[1]/div/div[13]/div/div/ul/li”

that keyword with that xpath returns an empty list, and i dont know why

Well that xpath looks really fragile. You could verify in the browser developer console if that xpath matches the elements you are expecting.

Hard to say how to fix xpath without seeing the website.

and i got this:


but i guess if i want all of them i should go for this:


whiout [1]

am i right?

Does locator css:div.ac_results li or xpath://div[@class=“ac_results”]//li work ?

1 Like

You should be looking for minimal matching locator that can be relied on.

That Copy XPath from the browser tools in general does not produce that good xpaths. There is an excellent article in our documentation on this topic https://robocorp.com/docs/development-guide/browser/how-to-find-user-interface-elements-using-locators-in-web-applications#what-to-click-that-is-the-question

And you could try some inspectors

  1. Robocorp Recorder - this is an Chrome Extension which can produce even sample Robot code for you.
  2. Selectors Hub - this is also an Chrome Extension which you can use to copy better XPaths than default browser developer tools
  3. There is an inspector in the Lab also, which is accessible via UI Locators tab on the right vertical tab.
1 Like

i solved it
thank you