How To Read Values Web-Table Row by Row

Hi Everyone!

Is there a walkaround to get the values/text row by row from a web-table. Below is the sample table html:

Activity User Channel Date Time
" Logs " User 201 Internal Apr 24, 2023 08:17 AM
" Level 3 Logs " User 201 Internal Apr 23, 2023 09:17 PM

Below is the code that I’ve written so far.

${rows} =    Get Element Count    //table[@id="Session Details"]/tbody/tr[position()>1]

FOR    ${row_num}    IN RANGE    ${rows}
    ${row} =    Get WebElement    //table[@id="Session Details"]/tbody/tr[position()>1][${row_num}]
    @{cells} =    Get WebElements    //table[@id="Session Details"]/tbody/tr[position()>1][${row_num}]/td
    ${data} =    Create List
    FOR    ${cell}    IN    @{cells}
        Sleep    3s
        Wait Until Element Is Visible    ${cell}
        ${text} =    Get Element Attribute    ${cell}    innerText
        ${data} =    Evaluate    [cell.text for cell in $cells]
        Log    ${cell} contains text: ${text}
    END
    Log    Row ${row_num} data: ${data}
END

Robot Framework Version - 3.1.2
Robot Framework Selenium Library Version - 3.3.1

The error that I’m getting is: No Keyword with name FOR found.

I even tried with old way of using for loop i.e. : For but got the same error as above
Can anyone guide me where I’m making mistake in my code?

Hi @Gautam,

FOR is part of the Robot Framework syntax since Version 3.1, so not sure why it’s not working in 3.1.2. As that is a very old version of Robot Framework (current version is 6.0.2), your best option is probably to try a more recent version as the issue has probably already been fixed.

Dave.

I did try to reproduce your problem but with modern RF and SeleniumLibrary. This is a modified copy of your test, which passes in RF 6.0.2 and SeleniumLibrary 6.0.0:

*** Settings ***
Library           SeleniumLibrary
Library           OperatingSystem

*** Test Cases ***
Test Loop
    [Setup]    Prepare Page
    ${rows}=    Get Element Count    //table[@id="Session Details"]/tbody/tr    # //table[@id="Session Details"]/tbody/tr[position()>1]
    FOR    ${row_num}    IN RANGE    1    ${rows+1}
        # ${row}= \ \ \ Get WebElement \ \ \ //table[@id="Session Details"]/tbody/tr[${row_num}]
        @{cells}=    Get WebElements    //table[@id="Session Details"]/tbody/tr[${row_num}]
        ${data}=    Create List
        FOR    ${cell}    IN    @{cells}
            Sleep    3s
            Wait Until Element Is Visible    ${cell}
            ${text}=    Get Element Attribute    ${cell}    innerText
            ${data}=    Evaluate    [cell.text for cell in $cells]
            Log    ${cell} contains text: ${text}
        END
        Log    Row ${row_num} data: ${data}
    END
    [Teardown]    Cleanup Test

*** Keywords ***
Prepare Page
    ${content}=    Set Variable    <table id="Session Details">\n \ \ \ <thead>\n \ \ \ \ \ \ \ <tr>\n \ \ \ \ \ \ \ \ \ \ \ <th> Activity </th>\n \ \ \ \ \ \ \ \ \ \ \ <th> User </th>\n \ \ \ \ \ \ \ \ \ \ \ <th> Channel </th>\n \ \ \ \ \ \ \ \ \ \ \ <th> Date </th>\n \ \ \ \ \ \ \ \ \ \ \ <th> Time </th>\n \ \ \ \ \ \ \ </tr>\n \ \ \ </thead>\n \ \ \ <tbody>\n \ \ \ \ \ \ \ <tr>\n \ \ \ \ \ \ \ \ \ \ \ <td>\n \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ " Logs "\n \ \ \ \ \ \ \ \ \ \ \ </td>\n \ \ \ \ \ \ \ \ \ \ \ <td>\n \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ <a> User 201 </a>\n \ \ \ \ \ \ \ \ \ \ \ </td>\n \ \ \ \ \ \ \ \ \ \ \ <td> Internal </td>\n \ \ \ \ \ \ \ \ \ \ \ <td> Apr 24, 2023 </td>\n \ \ \ \ \ \ \ \ \ \ \ <td> 08:17 AM </td>\n \ \ \ \ \ \ \ </tr>\n \ \ \ \ \ \ \ <tr>\n \ \ \ \ \ \ \ \ \ \ \ <td>\n \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ " Level 3 Logs "\n \ \ \ \ \ \ \ \ \ \ \ </td>\n \ \ \ \ \ \ \ \ \ \ \ <td>\n \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ <a> User 201 </a>\n \ \ \ \ \ \ \ \ \ \ \ </td>\n \ \ \ \ \ \ \ \ \ \ \ <td> Internal </td>\n \ \ \ \ \ \ \ \ \ \ \ <td> Apr 23, 2023 </td>\n \ \ \ \ \ \ \ \ \ \ \ <td> 09:17 PM </td>\n \ \ \ \ \ \ \ </tr>\n \ \ \ </tbody>\n</table>
    Set Test Variable    \${FILE}    .${TEST_NAME}_experiment.html
    Create File    ${FILE}
    Append To File    ${FILE}    <html>\n \ \ \ <title>Example</title>\n \ \ \ <body>${content}</body>\n</html>
    Open Browser    file:///${CURDIR}${/}${FILE}    Firefox

Cleanup Test
    Remove File    ${CURDIR}${/}${FILE}
    Close All Browsers
1 Like

With the same test file, and RF 3.2.1, I get the error at step Wait Until Element Is Visible :

Variable '${cell}' not found. Did you mean: 
   ${cells}

It ignored completely the inner FOR.

Like others said, RF 3.1.2 (and 3.2.1) don’t allow nested FOR loops.

1 Like

Thanks for your time and for your guidance @damies13

1 Like

Thanks for your time and for your guidance @HelioGuilherme66

1 Like