DMC
DMC

Reputation: 219

get all links and iterate through list (robot framework)

i want to check for all items on some inbox and open them one by one until there is no more (will do some processing per item, so they dissapear after it is visited). the items are workflow based, so when it is opened they will be processed and dissapear from the inbox.

how can i make robot make a list of these items and open them one by one? is this the right approach for such task? i'm using robotframework in eclipse w python

the //span/a catches all relevant links in the inbox (10)enter image description here

i have the following code which gets all links and puts them in a list, but i dont know what the best approach is to Open them one by one (return to inbox each time after a link was opened and some X process has been done).

Wait Until Element is Visible     xpath=//a/span
# Count number of links on page
${AllLinksCount}=    Get Matching Xpath Count    xpath=//a/span

# Log the count of links
Log    ${AllLinksCount}

# Create a list to store the link texts
@{LinkItems}    Create List

# Loop through all links and store links value that has length more than 1 character
: FOR    ${INDEX}    IN RANGE    1    ${AllLinksCount}
\    Log    ${INDEX}
\    ${lintext}=    Get Text    xpath=(//a/span)[${INDEX}]
\    Log    ${lintext}
\    ${linklength}    Get Length    ${lintext}
\    Run Keyword If    ${linklength}>1    Append To List    ${LinkItems}    ${lintext}
${LinkSize}=    Get Length    ${LinkItems}
Log    ${LinkSize}

Comment    Print all links
: FOR    ${ELEMENT}    IN    @{LinkItems}
\    Log    ${ELEMENT}

Upvotes: 1

Views: 5955

Answers (1)

becixb
becixb

Reputation: 373

If you want to go back to the page where you were, you could get the location before clicking on the link, and then go back to that url after processing. you don't even need to create a link test list for this. and for your xpath, make it //a/span[string-length(text())>1] so that you don't need the run keyword if keyword anymore:

Wait Until Element is Visible
xpath=//a/span[string-length(text())>1]
${AllLinksCount}=    Get Matching Xpath Count    xpath=//a/span[string-length(text())>1]
Log    ${AllLinksCount}
: FOR    ${INDEX}    IN RANGE    1    ${AllLinksCount}
\    Log    ${INDEX}
\    ${currUrl}    get location
\    click element    xpath=(//a/span[string-length(text())>1])[1]
\    do processing here
\    go to    ${currUrl}

If the URL changes, then you just have to go back to that page again via however you did before.

Upvotes: 2

Related Questions