python+selenium scroll bar/inline scroll bar slide down circularly, judge whether it slides to the bottom

1. The browser comes with an outer scroll bar: The idea is to use scrollBy to pull down the scroll bar, and then scrollTop will always change. When scrollTop does not change, the explanation is over. Just jump out of the loop.

Code:

import time
from selenium import webdriver
 
driver =webdriver.Chrome()
driver.maximize_window()
driver.get('http://www.baidu.com') 
driver.find_element_by_id('kw').send_keys('python滚动条的高度')
time.sleep(1)
driver.find_element_by_id('su').click()
time.sleep(2)
#定义一个初始值
temp_height=0
 
while True:
    #循环将滚动条下拉
    driver.execute_script("window.scrollBy(0,300)")
    #sleep一下让滚动条反应一下
    time.sleep(2)
    #获取当前滚动条距离顶部的距离
    check_height = driver.execute_script("return document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;")
    #如果两者相等说明到底了
    if check_height==temp_height:
        break
    temp_height=check_height
    print(check_height)
driver.quit()

operation result:

300
600
900
1199

Insert picture description here
2. Embedded scroll bar, scroll down to the bottom.
Code:
The demo.html code of the embedded scroll bar is at https://blog.csdn.net/zhaoweiya/article/details/107162825

import time
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('file:///C:/Users/18210/Desktop/demo.html')
# 定义一个初始值
temp_height = 0
x=20
y=20
while True:
    js1 = "var q=document.getElementsByClassName('scroll')[0].scrollTop={}".format(x)
    driver.execute_script(js1)
    time.sleep(2)
    x+=y
    check_height = driver.execute_script(
        "return document.getElementsByClassName('scroll')[0].scrollTop;")
    if check_height == temp_height:
        break
    temp_height = check_height
    print(check_height)
driver.quit()

operation result:

20
40
60
80
85

Guess you like

Origin blog.csdn.net/zhaoweiya/article/details/108996126