You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何点击class为data-command的动态元素集合中的最后一个元素?

修改Robot Framework代码实现点击最后一个动态元素

直接调整代码,去掉循环逻辑,改为获取元素列表后直接定位最后一个元素点击即可,具体修改如下:

# 获取所有class为data-command的元素
@{element_commands}=    Get WebElements    class:data-command
# 取列表最后一个元素(Robot Framework支持用索引-1直接获取最后一项)
Click Element    ${element_commands[-1]}
Sleep    5s
Run Keyword And Warn On Failure    Page Should Contain    ${graph}

补充说明:

  • Robot Framework的列表支持负索引,${element_commands[-1]}直接指向列表的最后一个元素,完美适配元素数量动态变化的场景,无需依赖绝对路径或固定循环次数。
  • 如果需要兼容旧版本的Robot Framework(部分旧版本可能不支持负索引),可以通过获取列表长度来定位最后一个元素:
@{element_commands}=    Get WebElements    class:data-command
${count}=    Get Length    ${element_commands}
# 索引从0开始,最后一个元素的索引是长度减1
Click Element    ${element_commands[${count}-1]}
Sleep    5s
Run Keyword And Warn On Failure    Page Should Contain    ${graph}

内容的提问来源于stack exchange,提问作者Aditya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 05:05:18