python中的selenium實現(xiàn)自動向下滾動頁面并指定最大滑動距離
更新時間:2022年02月11日 16:07:33 作者:呆萌的代Ma
這篇文章主要介紹了python中的selenium實現(xiàn)自動向下滾動頁面并指定最大滑動距離,下文有關(guān)selenium的資料介紹有一定的參考價值,需要的小伙伴可以參考一下
需要selenium控制的chrome向下滑動,自動加載一些內(nèi)容,核心代碼是:
browser.execute_script("window.scrollBy(0,300)")這行可以向下滑動300個像素
需要的工具函數(shù)如下:
def roll_window_to_bottom(browser, stop_length=None, step_length=500):
? ? """selenium 滾動當(dāng)前頁面,向下滑
? ? :param browser: selenium的webdriver
? ? :param stop_length: 滑動的最大值
? ? :param step_length: 每次滑動的值
? ? """
? ? original_top = 0
? ? while True: ?# 循環(huán)向下滑動
? ? ? ? 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 滾動當(dāng)前頁面,向下滑
? ? :param browser: selenium的webdriver
? ? :param stop_length: 滑動的最大值
? ? :param step_length: 每次滑動的值
? ? """
? ? original_top = 0
? ? while True: ?# 循環(huán)向下滑動
? ? ? ? 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') ?# 設(shè)置
? ? 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()
到此這篇關(guān)于python中的selenium實現(xiàn)自動向下滾動頁面并指定最大滑動距離的文章就介紹到這了,更多相關(guān)selenium實現(xiàn)自動向下滾動頁面并指定最大滑動距離內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實現(xiàn)遞歸查找某個路徑下所有文件中的中文字符
這篇文章主要為大家詳細介紹了python實現(xiàn)遞歸查找某個路徑下所有文件中的中文字符,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08
Python Flask請求擴展與中間件相關(guān)知識總結(jié)
今天帶大家學(xué)習(xí)的是關(guān)于Python Flask的相關(guān)知識,文章圍繞著Flask請求擴展與中間件的知識展開,文中有非常詳細的介紹,需要的朋友可以參考下2021-06-06
詳談在flask中使用jsonify和json.dumps的區(qū)別
下面小編就為大家分享一篇詳談在flask中使用jsonify和json.dumps的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03

