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

2024-05-20 13:16:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将第n项(即第3项)记录在我拥有的列表中。我通过循环一组web元素来获得列表。我的代码如下:

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},它似乎无法正常工作。我收到以下消息:

[<;selenium.webdriver.remote.webelement.webelement(session=“1f34fac642d44bb02f0c2b5c9a84c6f”,element=“245f8875-f545-4037-83c6-eef160bc285”)>;]

欢迎帮助


Tags: of代码textweb元素列表getcheck
2条回答

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

 [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

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

How do i get the nth element from the list in robotframework

要获取列表的第n个元素,请与python中的操作基本相同:

*** Variables ***
@{list}  one  two  three  four

*** Test Cases ***
Example
    Should be equal  ${list}[2]  three

这在robot framework用户指南中标题为Accessing list and dictionary variables的一节中进行了描述

相关问题 更多 >