Search code examples
seleniumselenium-webdriverrobotframework

How to get All Text in robot framework ?


Consider the following source code,

<div id="groupContainer" class="XXXXXX">

<ul id="GroupContactListWrapper" class="list-wrapper">
    <li class="contactNameItemContainer">

        <div class="contactNameItem">
            <span class="name">Name1</span>
        </div>

    </li>
    <li class="contactNameItemContainer">

        <div class="contactNameItem">
            <span class="name">Name2</span>
        </div>

    </li>
</ul>

</div>

How do i retreive the two names (Name1,Name2) in a list variable ? I tried the following xpath for a "Get Text" keyword, but only returns the first one.

//div[@id='groupContainer']//li[@class='contactNameItemContainer']//span

Please suggest


Solution

  • You could iterate over the elements as below:

    ${xpath}=    Set Variable    //div[@id='groupContainer']//li[@class='contactNameItemContainer']//span
    ${count}=    Get Matching Xpath Count    ${xpath}
    ${names}=    Create List
    :FOR    ${i}    IN RANGE    1    ${count} + 1
    \    ${name}=    Get Text    xpath=(${xpath})[${i}]
    \    Append To List    ${names}    ${name}
    

    This works but is rather slow when there are many matches.