6

I'm choosing a random value from a list to perform some actions over it like Run Keyword If, Exit For Loop If, click element, wait untill page contains and all. Now I'm not able to exit the for loop after the condition is being met but what's happening is that is starts from the beginning conditions that are set for value Batman.

Already tried :break & Exit For Loop keywords but they didn't work out for me

@{Hero}  Batman  Superman  Ironman

View All Details of Superhero
 ${value} =  Evaluate  random.choice($Hero)  random
    input text  ${SEARCH_BAR}  ${value}
    log to console  \nvalue: ${value}
    click element  ${SEARCH_BUTTON}

    Run Keyword If  '${value}' == 'Batman'  click element  ${P1}
    ...  ELSE IF  '${value}' == 'Superman'  click element  ${P2}
    ...  ELSE IF  '${value}' == 'Ironman'  click element  ${P3}
    ...  ELSE  log to console  condition didn't met

    :For  ${value}  IN  @{Hero}
        \  Log    ${value}
        \  Run Keyword If  '${value}' == 'Batman'  sleep  5s
        \  EXIT FOR LOOP IF  '${value}' == 'Batman'
        \  click element  //*[@href='#external']
        \  click element  ${BASIC_INFO}
        \  wait until page contains  Summary
        \  click element  ${RELATIONSHIP}
        @{expected_relationship_result}  create list  Catwoman  Robin
        list should contain value  @{expected_relationship_result}
        \  Log  condition didn't met

        \  Run Keyword If  '${value}' == 'Superman'  click element
        \  click element  //*[@href='#external']
        \  click element  ${BASIC_INFO}
        \  wait until page contains  Summary
        \  click element  ${RELATIONSHIP}
        @{expected_relationship_result}  create list  Wonderwomen  Lois Lane
        list should contain value  @{expected_relationship_result}  
        \  Log  condition didn't met

        \  Run Keyword If  '${value}' == 'Ironman'  sleep  5s  
        \  EXIT FOR LOOP IF  '${value}' == 'Ironman'
        \  click element  //*[@href='#external']  
        \  click element  ${BASIC_INFO}
        \  wait until page contains  Summary
        \  click element  ${RELATIONSHIP}
        @{expected_relationship_result}  create list  Wonderwomen  Lois Lane
        list should contain value  @{expected_relationship_result}
        \  Log  condition didn't met


I'm not able to figure out when the condition is already been met then why Exit For Loop If is not working.
2
  • The '\' is depreciated from robot 3.2 Commented Jun 5, 2019 at 14:56
  • Can you add the console output of what is happening? Commented Jun 7, 2019 at 18:11

1 Answer 1

8

Solution 1:

FOR    ${value}    IN    @{Hero}
    Do your stuff
    Exit For Loop IF    "${value}" == "${Batman}"
    Do your stuff
END

Can you try above syntax. This is latest for loop syntax. Its working for me and should work for you too. Use SeleniumLibrary latest version.

Solution 2: Old Syntax(this will also work)

: FOR    ${value}    IN    @{Hero}
\    Do your stuff
\    Exit For Loop IF    "${value}" == "${Batman}"
\    Do your stuff
Sign up to request clarification or add additional context in comments.

1 Comment

Quick update to this topic, the :-> :FOR and Exit For Loop IF are deprecated since RF 5.* (specially Exit For Loop) : FOR ${value} IN @{Hero} \ Do your stuff \ Exit For Loop IF "${value}" == "${Batman}" \ Do your stuff Instead of Exit For Loop, you can do : FOR ${value} IN @{Hero} \ Do your stuff \ IF "${value}" == "${Batman}" BREAK \ Do your stuff END Link to documentation 👉robotframework.org/robotframework/latest/libraries/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.