编写用例时,碰到带有滚动条的下拉列表,目标选项不在第一页,需要翻页找到它。
Focus关键词实验结果不生效,后采用 Execute Javascript 关键词,执行Javascript脚本翻页。样例如下。
Scroll Selector To
[Arguments] ${locator} # 传入目标选项的locator
${dropdown} Evaluate '/'.join(u"${locator}".split('/')[:-1]) # 通过目标选项,定位到下拉列表
${count} Get Element Count ${dropdown}/dd # 确定下拉列表的选项总数
${pagecount} Evaluate int('${count}')/7 # 每一页包含7个选项,确定总页数
${width} ${height} Get Element Size ${dropdown} # 得到下拉列表在运行环境中的高度
: FOR ${i} IN RANGE ${pagecount} # 翻页寻找目标选项
\ ${status} ${value} Run Keyword And Ignore Error Element Should Be Visible ${locator}
\ Exit For Loop If '${status}' == 'PASS' # 如果找到选项,跳出循环,停止滚动
\ Execute Javascript document.getElementsByTagName('dl')[2].scrollTop=${height} # 如果目标选项不可见,滚动滚动条,根据之前得到的高度,每次滚动一页,最多滚动到最后一页。
解决过程中,学习到一些其他的滚动页面方法。
操作浏览器滚动条
window.scrollBy(0, document.body.scrollHeight)
window.scrollTo(0, document.body.scrollHeight)
document.body.scrollHeight表示滑到最底部,如果要滑到什么位置,通过填写值来调试,比如
Execute Javascript window.scrollTo(0, 50)
参考:
https://blog.csdn.net/a5650892/article/details/78430303
https://www.cnblogs.com/lgqboke/p/8277170.html
原文:https://blog.csdn.net/qq_41963758/article/details/81147304