【问题标题】:How do i get the nth element from the list in robotframework我如何从机器人框架中的列表中获取第 n 个元素
【发布时间】:2021-12-05 12:52:57
【问题描述】:

我正在尝试在我拥有的列表中记录第 N 个(即第 3 项)项目。我通过遍历一组网络元素来获得列表。我的代码如下:

check order of list
    @{list}=  Get WebElements     xpath://*[@id="demo-tabpane-list"]/div
    FOR    ${elements}    IN    @{list}
        ${text}=     Get Text    ${elements}
        Log To Console  ${text}
    END

现在它完美地记录了循环中的所有文本,但如果我尝试记录@{list},我似乎无法让它工作,我收到以下消息:

[]

欢迎帮助!

【问题讨论】:

  • 这看起来不像 Python。
  • 它的机器人框架在 python 上运行。

标签: python robotframework


【解决方案1】:

我通过导入字符串库来修复它。并将我的代码更改为:

 [arguments]    @{list}
    FOR    ${elements}    IN    @{list}
        ${text}=     Get Text    ${elements}
        @{numberList}=  Split String    ${text}  \n
        ${Lenght}=      Get length  ${numberList}
       Should be equal     ${numberList}[0]    One
       Should be equal     ${numberList}[8]    Nine
    END

我将文本拆分成松散的元素,然后放入一个数组中,这样我就可以按索引读取它们。

【讨论】:

    【解决方案2】:

    如何从robotframework的列表中获取第n个元素

    要获取列表的第 n 个元素,其操作与在 python 中的操作几乎相同:

    *** Variables ***
    @{list}  one  two  three  four
    
    *** Test Cases ***
    Example
        Should be equal  ${list}[2]  three
    

    这在机器人框架用户指南中标题为Accessing list and dictionary variables的部分中进行了描述

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-24
      • 2020-10-29
      • 2020-01-12
      • 2017-07-23
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      相关资源
      最近更新 更多