如何在每次执行循环时更改选择器?

时间:2018-11-15 11:22:53

标签: robotframework

在下面的脚本中,我希望每次执行循环时都更改变量${drop_down_items}的值。

div.col:nth-child(**2**) > div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > combo-box:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div

在上面的定位器中,我希望在每次执行循环时将nth-child值从2递增到3,从3递增到4,依此类推。

*** Variables ***
${combo_boxes}      css=div.col > div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > combo-box:nth-child(1) > div:nth-child(1)
${drop_down_items}  css=div.col:nth-child(2) > div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > combo-box:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div
*** Keywords ***
Loop
  @{combo_boxes}=  Get WebElements  ${combo_boxes}
  :FOR    ${each}     IN      @{combo_boxes}
  \  Click Element  ${each}
  \  Loop A
  \  Sleep  0.5s
  \  Click Element  ${each}

Loop A
  @{get_role_list}=  Get WebElements  css=div.col:nth-child(2) > div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > combo-box:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div
  :FOR    ${each}     IN      @{get_role_list}
  \    Sleep  0.5s
  \    Click Element  ${each}
  \    Sleep  0.5s
  \    Run Keyword If  '${each}'!='EXIT'  Click Element  ${combo_boxes}

2 个答案:

答案 0 :(得分:0)

解决方案是添加另一个循环层。 《 Robot Framework指南》中有一节介绍了Robot Framework提供的不同循环功能。特别是在For-in-range section处有一个循环。这是一个可以帮助您解决问题的示例。

Copy only when installing

答案 1 :(得分:0)

Loop
  @{combo_boxes}=  Get WebElements  ${combo_boxes}
  ${INTEGER} =    Set Variable  2
  Set Suite Variable  ${INTEGER_A}  ${INTEGER}
  :FOR    ${each}     IN      @{combo_boxes}
  \  Click Element  ${each}
  \  ${roles_list}=  Set Variable    css=.list > div:nth-child(${INTEGER_A}) > 
   div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > combo-box:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div
   \  Loop A  ${roles_list}  ${each}
   \  ${INTEGER_A}=  Evaluate    ${INTEGER_A} + 1
   \  Sleep  0.5s
   \  Click Element  ${each}


Loop A
  [Arguments]  ${roles_list}  ${combo}
  @{get_role_list}=  Get WebElements  ${roles_list}
  :FOR    ${each}     IN      @{get_role_list}
  \    Click Element "${each}"
  \    Run Keyword If  '${each}'!='EXIT'  Click Element  ${combo}