網站首頁 編程語言 正文
需要selenium控制的chrome向下滑動,自動加載一些內容,核心代碼是:
browser.execute_script("window.scrollBy(0,300)")
這行可以向下滑動300個像素
需要的工具函數如下:
def roll_window_to_bottom(browser, stop_length=None, step_length=500): ? ? """selenium 滾動當前頁面,向下滑 ? ? :param browser: selenium的webdriver ? ? :param stop_length: 滑動的最大值 ? ? :param step_length: 每次滑動的值 ? ? """ ? ? original_top = 0 ? ? while True: ?# 循環向下滑動 ? ? ? ? if stop_length: ? ? ? ? ? ? if stop_length - step_length < 0: ? ? ? ? ? ? ? ? browser.execute_script("window.scrollBy(0,{})".format(stop_length)) ? ? ? ? ? ? ? ? break ? ? ? ? ? ? stop_length -= step_length ? ? ? ? browser.execute_script("window.scrollBy(0,{})".format(step_length)) ? ? ? ? time.sleep(0.5 + random.random()) ?# 停頓一下 ? ? ? ? check_height = browser.execute_script( ? ? ? ? ? ? "return document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;") ? ? ? ? if check_height == original_top: ?# 判斷滑動后距頂部的距離與滑動前距頂部的距離 ? ? ? ? ? ? break ? ? ? ? original_top = check_height
使用示例:
from selenium import webdriver import time import random def roll_window_to_bottom(browser, stop_length=None, step_length=500): ? ? """selenium 滾動當前頁面,向下滑 ? ? :param browser: selenium的webdriver ? ? :param stop_length: 滑動的最大值 ? ? :param step_length: 每次滑動的值 ? ? """ ? ? original_top = 0 ? ? while True: ?# 循環向下滑動 ? ? ? ? if stop_length: ? ? ? ? ? ? if stop_length - step_length < 0: ? ? ? ? ? ? ? ? browser.execute_script("window.scrollBy(0,{})".format(stop_length)) ? ? ? ? ? ? ? ? break ? ? ? ? ? ? stop_length -= step_length ? ? ? ? browser.execute_script("window.scrollBy(0,{})".format(step_length)) ? ? ? ? time.sleep(0.5 + random.random()) ?# 停頓一下 ? ? ? ? check_height = browser.execute_script( ? ? ? ? ? ? "return document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;") ? ? ? ? if check_height == original_top: ?# 判斷滑動后距頂部的距離與滑動前距頂部的距離 ? ? ? ? ? ? break ? ? ? ? original_top = check_height def main(): ? ? option = webdriver.ChromeOptions() ? ? option.add_argument('lang=zh_CN.UTF-8') ?# 設置 ? ? browser = webdriver.Chrome(chrome_options=option, desired_capabilities={"page_load_strategy": "none"}) ? ? browser.get("http://news.baidu.com/") ? ? roll_window_to_bottom(browser, stop_length=700) if __name__ == '__main__': ? ? main()
原文鏈接:https://blog.csdn.net/weixin_35757704/article/details/122763602
相關推薦
- 2022-05-17 qt 錯誤GL/gl.h: No such file or directory的解決方法
- 2022-03-22 C++using聲明和using編譯指令_C 語言
- 2022-03-31 用Python實現石頭剪刀布游戲_python
- 2022-06-08 ASP.NET?Core中的靜態文件_基礎應用
- 2022-09-30 Python中4種實現數值的交換方式_python
- 2022-01-27 @ConfigurationProperties放在類上跟放在方法上有什么區別
- 2022-09-22 Mybaits一級緩存和二級緩存分別是什么,區別是什么?
- 2022-12-21 redis?setIfAbsent返回null的問題及解決_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支