'Get the list of items in table

This is regarding Robot Framework scripting.

I want to get the items in the column "Model Name" and then verify user clicks on column header to sort the items correctly.

How do I get the list of items? The items reside in <span class=""> , sample HTML at below:

screenshot

<tr data-row-key="254104e218ea47f9879a270a3b688da3"
    class="ant-table-row ant-table-row-level-0">
<td class="ant-table-cell ant-table-row-expand-icon-cell">
    <button type="button"
            class="ant-table-row-expand-icon ant-table-row-expand-icon-collapsed"
            aria-label="Expand row">
    </button>
</td>
<td class="ant-table-cell ant-table-column-sort">
    <span><span class="">autotestmodel-0844-v1-8k</span></span>
</td>
...
</tr>


Solution 1:[1]

You can try CSS selector on the "nth" child inside the 2nd <td> element within each row.

document.querySelectorAll('table tr td:nth-child(2)');

This will let you iterate over each <td> element, that is placed in the 2nd place in its <tr> container, and return a node list of all the relevant cells. On this node list you can then iterate and use the inner text as you wish.

document.querySelectorAll('table tr td:nth-child(2)').forEach( item => {
  // do whatever you want with each node's text
  console.log(item.innerText);
})

Solution 2:[2]

Interact with the table by using selector table tr td:nth-child(2) Get each of element then Append it to a list.

*** Settings ***
Library  Browser
Library  String
Library  Collections
Resource            ../Resources/BrowserFunctions.robot
Suite Setup         Start New Browser
Suite Teardown      Close Browser



*** Test Cases ***
001-Models
    Open New Page To Server
    Navigate To Models Module

   ${elements} =    Get Elements    table tr td:nth-child(2)
   FOR   ${elem}   IN   @{elements}
      ${text}=    Get Text    ${elem}
      Log To Console    Model is:${text}
    END

To append all values to a list:

    ${elements} =    Get Elements    table tr td:nth-child(2)
    ${LexiconList}=    Create List
    FOR    ${elem}    IN    @{elements}
    ${text}=    Get Text    ${elem}
    ${new elem}=     Set Variable    ${text}
    Append To List    ${LexiconList}    ${new elem}
    END
    Log To Console    Lexicon List is: ${LexiconList}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Narxx
Solution 2