Home > Enterprise >  How do i get the nth element from the list in robotframework
How do i get the nth element from the list in robotframework

Time:10-18

I am trying to log the Nth (i.e 3rd item) item in a list i have. I get the list by looping over an array of web elements. My code is as follows:

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

Now it logs all the text in the loop perfectly but i cant seem to get it to work if i try to log @{list} i get the following message:

[<selenium.webdriver.remote.webelement.WebElement (session="1f34fac642dd44bb02f0c2b5c9a84c6f", element="245f8875-f545-4037-83c6-eef1600bc285")>]

help is welcome!

CodePudding user response:

I fixed it by importing the string library. And changing my code to:

 [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

I split the text into loose elements that get put into an array so i can read them by index.

CodePudding user response:

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

To get the nth element of a list, do it pretty much the same as you do in python:

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

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

This is described in the robot framework user guide in a section titled Accessing list and dictionary variables

  • Related