0
votes

I would like to save elements into a list, and then run a FOR loop to iterate through them.

I have tried the following:

*** Keywords ***
User Claims Tasks
    @{tasks}=    Get WebElements    ${claim}
    FOR    ${task}    IN    @{tasks}
        Click Element    ${task}    
        ${missing}=    Run Keyword And Return Status    Element Should Be Visible    ${miss}
        Run Keyword If    '${missing}' == 'True'    Click Element    ${miss}
        ${continue}=      Run Keyword And Return Status    Element Should Be Visible    ${cont}
        Run Keyword If    '${continue}' == 'True'    Click Element    ${cont}
    END

However, this does not work. It runs once, and then I get the error:

20200911 13:40:19.734 : FAIL : IndexError: list index out of range
20200911 13:40:19.737 : INFO : ${relation uid} = ('FAIL', 'IndexError: list index out of range')
20200911 13:40:19.742 : INFO : Relation UID:('FAIL', 'IndexError: list index out of range')
20200911 13:40:19.744 : INFO : Client Information:, 
Ending test:

Any ideas on how to get this work?

1
Please share which line throws this error, and the return value of this line @{tasks}= Get WebElements ${claim} as it is shown in the log.html. - Bence Kaulics
@{tasks} = [ <selenium.webdriver.remote.webelement.WebElement (session="03200ec34b66c1a5cea7e41f7c1e9905", element="31b953c1-d1d5-47de-bae3-402a6cd57160")> | <selenium.webdriver.remote.webelement.WebElement (ses... - rolez22
The error occurs when it wants to click the second task (beginning of 2 iteration - click element task). It cannot click on the task because: element is not attached to the page document. - rolez22
Try Click Element ${task.get_attribute("id")} it will not use the stale webelement but will click on the element based on the ID. It will work if the page refresh or whatever is happening is not changing the ID of the tasks. - Bence Kaulics
When you click on a task in the page, does it change? Like, the task list redraw? It looks so by the exception in the 3rd comment. If true, you have to rethink the logic - not to get all elements in the beginning - as they will change with interaction, but loop while there are still task elements, regetting them every time. - Todor Minakov

1 Answers

0
votes

The solution that worked for me:

*** Keywords ***
User Claims Tasks
    ${tasks}=    Get Element Count    ${claim}
    FOR    ${i}    IN RANGE    ${tasks}
        ${present}=    Run Keyword And Return Status    Element Should Be Visible    ${claim}  
        Run Keyword If   '${present}' == 'True'    Click Element    ${claim}  
        ${missing}=    Run Keyword And Return Status    Element Should Be Visible    ${drm}
        Run Keyword If    '${missing}' == 'True'    Click Element    ${drm}
        ${continue}=      Run Keyword And Return Status    Element Should Be Visible    ${cont}
        Run Keyword If    '${continue}' == 'True'  Click Element    ${cont}
    END