Search code examples
automationrobotframework

Robot Framework: How to parse blank text entries in a returned text list from a FOR Loop


I have a functioning Robot Framework test that scrapes the elements of a page and returns the link text. My problem is, some of these fields are empty and some of them have text. I do not care what the text is, however. This is an example of the output I get:

  1. "Link Text="
  2. "Link Text=" John Doe
  3. "Link Text=" Jane Doe

In this case, I would only want the program to return items 2 and 3, and not Log item 1. Here is my code to achieve this right now.

TEST
    ${Count}=       Get Element Count  //a
    Log To Console  Total= ${Count} \n
    FOR  ${INDEX}   IN RANGE  1  ${Count}-1
        ${text}=    Get Text  xpath=(//a)[${INDEX}]
        ${href}=    Run Keyword And Return Status  Get Element Attribute  xpath=(//a)[${INDEX}]    @href
        Run Keyword If  ${href}  Log To Console  ${INDEX}. "Link Text=" ${text}
        ...  ELSE  Log To Console  NONE
    END

So this does give me a pass, and I do get the link text I'm asking for. I just need to take out the blank entries. I know my loop is functional, but I can't figure out how to parse the blank fields. How can I do this? Any ideas? Please let me know, thanks so much!


Solution

  • In these situations I usualy use Get Length and test if greater than zero.

    Here is your modified example:

    TEST
        ${Count}=       Get Element Count  //a
        Log To Console  Total= ${Count} \n
        FOR  ${INDEX}   IN RANGE  1  ${Count}-1
            ${text}=    Get Text  xpath=(//a)[${INDEX}]
            ${size}=    Get Length    ${text}
            ${href}=    Run Keyword And Return Status    Get Element Attribute  xpath=(//a)[${INDEX}]    @href
            Run Keyword If    ${href} and ${size} > 0    Log To Console  ${INDEX}. "Link Text=" ${text}
            ...    ELSE    Log To Console  NONE
        END