user2371684
user2371684

Reputation: 1555

issue with for loops in robot framework

I am new to Robot Framework and I am trying to implement a for loop where I want to loop through and store all the text values of all a tags.

But I am only able to get the text of one single a tag.

This is my code:

*** Settings ***
Library    SeleniumLibrary


*** Variables ***
${url}  http://www.practiceselenium.com/practice-form.html


*** Test Cases ***
ExtractLinks
    alllinks

    close browser


*** Keywords ***
allLinks
    open browser    ${url}

    ${allLinksCount}    get element count    xpath://a
    log to console  ${allLinksCount}


    FOR     ${i}     IN      1  ${allLinksCount}

    ${linkText}     get text    xpath:(//a)[${i}]

    log to console    ${linkText}

    END

This is the output:

==============================================================================
getAllLinks
==============================================================================
ExtractLinks                                                          10
More

ExtractLinks                                                          | PASS |
------------------------------------------------------------------------------
getAllLinks                                                           | PASS |
1 test, 1 passed, 0 failed
==============================================================================

So there are 10 a tags on the page, but only one is shown, "More" in the output to the console.

Is it all possible to gather all a link text in list instead?

Upvotes: 0

Views: 742

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

FOR ${i} IN 1 ${allLinksCount} is equivalent to the python code for i in (1, 10). In other words, it will loop exactly twice.

If you're wanting to iterate over the range of numbers between 1 and ${allLinksCount} you should use IN RANGE.

Because xpath counts starting with one instead of zero, you'll need to adjust the numbers slightly:

FOR     ${i}     IN RANGE     1  ${allLinksCount}+1
    ${linkText}     get text    xpath:(//a)[${i}]
    log to console   ${linkText}
END

-or-

FOR     ${i}     IN RANGE     ${allLinksCount}
    ${linkText}     get text    xpath:(//a)[${i+1}]
    log to console   ${linkText}
END

Upvotes: 2

Related Questions