【问题标题】:How to read the keys from array using robot framework?如何使用机器人框架从数组中读取键?
【发布时间】:2026-02-14 13:00:01
【问题描述】:

我需要从数组中读取名为 value 的两个键。我能够阅读一把钥匙。在读取名为“value”的两个键时需要帮助

我用过 Robot Framework。

RobotFramework.robot:

 ${readvalues}=  [{u'count': 1, u'id': u'vegetable', u'value': u'veg'}, {u'count': 1, u'id': u'Non_vegetarian', u'value': u'Non_veg'}]

 ${read_prv_value}=     Set Variable     ${readvalue['value']}
 log to console   ${read_prv_value}   #prints Non-veg alone I expected Veg and Non Veg
  ${value_cnt}=    Get Length    ${readvalues}
  : FOR    ${item}    IN RANGE   0    ${value_cnt}
         \    ${readvalue}=    Set Variable     ${var[${item}]['value']}

我预计是 Veg 和 Non Veg,但实际的 o/p 是 Non Veg

【问题讨论】:

    标签: dictionary robotframework


    【解决方案1】:

    这不是两个字典的列表吗?然后您必须循环列表或使用提供列表索引的单独字典。

    Read with loop
    ${d1}=    Create Dictionary    count=1    id=vegetable    value=veg
    ${d2}=    Create Dictionary    count=1    id=Non_vegetarian    value=Non_veg
    ${readvalues}=  Create List    ${d1}    ${d2}
    : FOR    ${item}    IN    @{readvalues}
    \    ${value}=    Set Variable     ${item}[value]
    \    Log To Console    ${value}
    
    Read without loop
        ${d1}=    Create Dictionary    count=1    id=vegetable    value=veg
        ${d2}=    Create Dictionary    count=1    id=Non_vegetarian    value=Non_veg
        ${readvalues}=  Create List    ${d1}    ${d2}
        ${value1}=    Set Variable     ${readvalues}[0][value]
        Log To Console    ${value1}
        ${value2}=    Set Variable     ${readvalues}[1][value]
        Log To Console    ${value2}
    

    两个关键字记录:

    veg
    Non_veg
    

    【讨论】:

    • 你能帮我解决我尝试使用 for 循环的代码吗?