selenium页面滚动加载到底部
python
import time
# 方法:滚动到页面底部
def scroll_to_bottom(driver):
last_height = driver.execute_script("return document.documentElement.scrollHeight")
print(f"Initial Document Height: {last_height}")
while True:
print("Scrolling down...")
# 向页面底部滚动
driver.execute_script("window.scrollTo(0, document.documentElement.scrollHeight);")
# 等待页面加载
time.sleep(3)
# 获取新的页面高度
new_height = driver.execute_script("return document.documentElement.scrollHeight")
print(f"New Document Height: {new_height}")
# 如果新高度和旧高度相同,说明已经到底
if new_height == last_height:
print("Reached bottom of the page.")
break
last_height = new_height