selenium操作滚动条滚动方法

一、应用背景
在UI自动化中存在某些特殊场景,需要拉动滚动条到底部或者滚动到某个位置 才可以触发按钮点击

二、使用方法
1、在selenium中的execute_script调用js方法
execute_script介绍:
在当前窗口/框架 同步执行javaScript

scrollTop:这个元素的顶部到视口可见内容(的顶部)的距离

滚动条在div内嵌的demo:


#获取内嵌div的位置 并设置滚动条移动的值
js="document.getElementById('agreementMain').scrollTop=12000"
# 调用js方法
driver.execute_script(js)

2、js的window.scrollTo()方法
window.scrollTo(0,10000)指定滑动的像素

driver.execute_script("window.scrollTo(0,10000)")
               

Guess you like

Origin blog.csdn.net/SKY_PLA/article/details/121470840