031、javascrip 操作元素 02、操作浏览器滚动条 scrollTo

 

更多资料参考:https://www.cnblogs.com/yoyoketang/p/6128655.html

 

一、javascrip 操作元素 :操作浏览器滚动条scrollTo

a、滚动到底部、 顶部

# Author:  Sky
# Email:   2780619724@qq.com
# Time:    2021/8/21 13:36
# Project: day01
# Module:  ch_05.py
# Environment: Python3.8.6 , Selenium3 环境 ( 3.141.0 版本)
# Environment: Chrome ( 92.0.4515.131, 正式版本) + chromedriver(92.0.4515.107版本)

from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get(
"http://49.235.92.12:8200/") time.sleep(2) # 滚动到底部 js_end = 'window.scrollTo(0,document.body.scrollHeight);' driver.execute_script(js_end) time.sleep(2) # 滚动到顶部 js_top = 'window.scrollTo(0,0);' driver.execute_script(js_top) time.sleep(2) driver.quit()

 

a、滚动到左部、 右部

# -*- coding:utf-8 -*-
# Author: Sky
# Email: 2780619724@qq.com
# Time: 2021/8/21 13:36
# Project: day01
# Module: ch_05.py
# Environment: Python3.8.6 , Selenium3 环境 ( 3.141.0 版本)
# Environment: Chrome ( 92.0.4515.131, 正式版本) + chromedriver(92.0.4515.107版本)

from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("http://49.235.92.12:8200/")
# driver.maximize_window()
driver.set_window_size(600, 600)

# 纵向滑动到一半
time.sleep(2)
js_end_1 = 'window.scrollTo(0,document.body.scrollHeight/2);'
driver.execute_script(js_end_1)

time.sleep(2)
# 纵向滚动到底部
js_end_2 = 'window.scrollTo(0,document.body.scrollHeight);'
driver.execute_script(js_end_2)


time.sleep(2)
# 横向滚动到右部
js_end_3 = 'window.scrollTo(document.body.scrollWidth,document.body.scrollHeight);'
driver.execute_script(js_end_3)


time.sleep(4)
# 横向滚动到左部
js_end_4 = 'window.scrollTo(0,document.body.scrollHeight);'
driver.execute_script(js_end_4)

time.sleep(4)
driver.quit()

 

posted @ 2021-08-20 19:56  空-山-新-雨  阅读(72)  评论(0编辑  收藏  举报