How to use dictionary instead of of list in robot framework

Hi Leo,

I think you were wanting to reply to @_daryl but tagged me instead.

what you could try; (this is a bit hackish but might do what you want)

Replace this line:

        ${key}=  Get From Dictionary  ${text_dict}  ${element}  # Get the key value from ${text_dict} 

with this:

        ${key}=  Evaluate    list(${text_dict})[${element}]

To explain what is happening here:

  • The Evaluate keyword can run any python code, but you can also use robot variables in the python code that’s evaluated with the Evaluate keyword
  • the list(${text_dict}) part used the python list function to convert the keys of a dictionary to a list of the key names
  • the [${element}] part is taking the nth item from the list we just created from the dictionary keys

While this may work it’s probably not the best way to do things as if there is java script running in the web page you are scraping this data from and it updates the page between when you added the key to the dictionary and when you update the value then there is a potential for a misalignment.

I would prefer to see you gather the keys to a list first then use the key as part of the xpath to get the value so you can be sure the key and value are related, only then add the key and value to the dictionary.

Perhaps the misalignment I mention will never happen and you’ll be lucky, I don’t know anything about the app your testing, but at least consider of you can select the value by the key or if you can select the key and value at the same time in the initial loop so your initial Set To Dictionary ${text_dict} ${list_text} ${EMPTY} can be replaced with Set To Dictionary ${text_dict} ${list_text} ${value}

Dave.

3 Likes