在Robot Framework中退出循环

时间:2019-10-23 06:31:59

标签: python python-3.x selenium robotframework

使用机器人框架,我试图创建一个FOR循环,在该循环中从列表中选择一个随机值。选择随机值后,将打开该值的页面,然后我要验证该方可用的数据,等等。然后,如果值匹配,则循环应退出。

当前,我能够选择一个随机值并转到该值页面,但是由于FOR循环从列表中找到另一个值,脚本无法执行,因此无法执行进一步的操作。

@{ORGANISATIONAL_NAME}  JSK MARKETING  KEDARNATH COMOTRADE  PONDICHERRY SRI LAKSHMI

View Basic Info of the Party
    ${value} =  Evaluate  random.choice($ORGANISATIONAL_NAME)  random
    input text  ${SEARCH_BAR}  ${value}
    log to console  \nvalue: ${value}
    click element  ${SEARCH_BUTTON}
    log  ${value}
    Run Keyword If  '${value}' == 'JSK MARKETING'  click element  ${JSK_MARKETING}
     ...  ELSE IF  '${value}' == 'KEDARNATH COMOTRADE'  click element  ${KEDARNATH_COMOTRADE}
     ...  ELSE IF  '${value}' == 'PONDICHERRY SRI LAKSHMI'  click element  ${PONDICHERRY_SHRI_LAKSHMI}
     ...  ELSE  log to console  condition didn't met
    FOR  ${value}  IN  @{ORGANISATIONAL_NAME}
        log to console  ${value}
        click element  ${BASIC_INFO}
        wait until page contains  Summary
        Exit For Loop If  '${value}' == 'JSK MARKETING'
        click element  ${BASIC_INFO}
        wait until page contains  Summary
        Exit For Loop If  '${value}' == 'KEDARNATH COMOTRADE'
        click element  ${BASIC_INFO}
        wait until page contains  Summary
        Exit For Loop If  '${value}' == 'PONDICHERRY SRI LAKSHMI'
    END

需要对从列表中选择的随机值进行验证。

1 个答案:

答案 0 :(得分:0)

您的问题是您首先选择一个随机值。 然后按创建顺序对列表进行迭代。

for循环的原因是什么? 因为我不明白您在循环中要做什么

编辑:

如果列表中仅存在这3个值,则可以执行以下操作:

View Basic Info Of The Party
    ${value} =  Evaluate  random.choice($ORGANISATIONAL_NAME)  random
    Input Text  ${SEARCH_BAR}  ${value}
    Log To Console  \nvalue: ${value}
    Click Element  ${SEARCH_BUTTON}
    Log  ${value}
    Run Keyword If  '${value}' == 'JSK MARKETING'  Click Element  ${JSK_MARKETING}
    ...  ELSE IF    '${value}' == 'KEDARNATH COMOTRADE'  Click Element  ${KEDARNATH_COMOTRADE}
    ...  ELSE IF    '${value}' == 'PONDICHERRY SRI LAKSHMI'  Click Element  ${PONDICHERRY_SHRI_LAKSHMI}
    Log To Console  ${value}
    Click Element  ${BASIC_INFO}
    Wait Until Page Contains  Summary

如果您要在列表中拥有更多值,并且只想在这3种情况之一中执行一项操作,请尝试执行以下操作:

View Basic Info Of The Party
    ${value} =  Evaluate  random.choice($ORGANISATIONAL_NAME)  random
    Input Text  ${SEARCH_BAR}  ${value}
    Log To Console  \nvalue: ${value}
    Click Element  ${SEARCH_BUTTON}
    Log  ${value}
    Run Keyword If  '${value}' == 'JSK MARKETING'  Click Element  ${JSK_MARKETING}
    ...  ELSE IF    '${value}' == 'KEDARNATH COMOTRADE'  Click Element  ${KEDARNATH_COMOTRADE}
    ...  ELSE IF    '${value}' == 'PONDICHERRY SRI LAKSHMI'  Click Element  ${PONDICHERRY_SHRI_LAKSHMI}
    ...  ELSE  Log To Console  condition didn\'t met
    Run Keyword If  '${value}' == 'JSK MARKETING' or '${value}' == 'KEDARNATH COMOTRADE' or '${value}' == 'PONDICHERRY SRI LAKSHMI'     Run Keywords
    ...  Log To Console  ${value}   AND
    ...  Click Element  ${BASIC_INFO}   AND
    ...  Wait Until Page Contains  Summary

我希望这会有所帮助。