使用 js 执行向下滚动,适用于那些需要滚动加载数据的页面
execute_script
执行一个带有return
语句时,会获取js
执行后返回的结果,这里用于获取页面高度
# 滚动js
js = "window.scrollTo(0,document.body.scrollHeight)"
# 获取页面高度 js
body_height_js = "return document.body.scrollHeight"
height = browser_object.execute_script(body_height_js)
while True:
print(height, "开始滚动...")
browser_object.execute_script(js)
time.sleep(1)
new_height = browser_object.execute_script(body_height_js)
# 每执行一次滚动条拖到最后,就进行一次参数校验,并且刷新页面高度
if new_height > height:
height = new_height
time.sleep(1)
else:
# 当页面高度不再增加的时候,我们就认为已经是页面最底部,结束条件判断
print(‘已到达页面底部,开始分析数据‘)
break
原文:https://www.cnblogs.com/gldsly/p/14646112.html