Selenium 滚动页面至元素可见的方法
作者:落晨 时间:2022-06-01 14:01:05
滚动页面
在自动化操作中,如果web页面过长,而我们需要的元素并不在当前可视页面中,那么selenium就无法对其进行操作;此时,我们就需要像平时操作浏览器一样来滚动页面,使我们需要操作的对象可见!
滚动页面的方法:
window.scrollBy()
window.scrollBy(0,500) 向下滚动500个像素
window.scrollBy(0,-500) 向上滚动500个像素
window.scrollBy(500,0) 向右滚动500个像素
window.scrollBy(-500,0) 向左滚动500个像素
使用方式:
在 开发者工具--Console中输入以上内容,即可实现页面滚动
示例:window.scrollBy(0,500) 向下滚动500个像素
Selenium中实现滚动页面
driver.execute_script('window.scrollBy()')
driver.execute_script("arguments[0].scrollIntoView();", ele)滚动至元素ele可见
代码示例:
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.implicitly_wait(10)
# 设置窗口大小
driver.set_window_size(800, 700)
driver.get('http://baidu.com')
# 百度输入框输入 selelnium python 回车
driver.find_element_by_id("kw").send_keys("selenium python\n")
time.sleep(2)
# 向下滚动200个像素
driver.execute_script('window.scrollBy(0,200)')
time.sleep(2)
# 滚动至元素ele可见位置
eles = driver.find_elements_by_css_selector('#rs table tr th a')
ele = eles[0]
driver.execute_script("arguments[0].scrollIntoView();",ele)
time.sleep(2)
# 向右滚动200个像素
driver.execute_script('window.scrollBy(200,0)')
time.sleep(2)
driver.quit()
来源:https://www.cnblogs.com/wilson-5133/p/10996810.html
标签:Selenium,滚动页面,元素可见
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python多线程同步Lock、RLock、Semaphore、Event实例
2023-08-03 20:47:15
![](https://img.aspxhome.com/file/2023/1/61461_0s.png)
Javascript浅拷贝与深拷贝实现
2013-07-16 22:47:46
Python利用Beautiful Soup模块创建对象详解
2023-05-17 23:24:58
Hibernate Oracle sequence的使用技巧
2023-07-06 05:18:42
python类继承与子类实例初始化用法分析
2023-03-31 17:14:24
ASP生成静态网页各种方法收集整理
2008-02-18 19:25:00
Insert into与AddNew哪一个更好?
2009-10-28 18:30:00
网站防止采集方法全攻略
2007-09-05 19:57:00
Golang中字符串(string)与字节数组([]byte)一行代码互转实例
2023-09-17 14:37:07
![](https://img.aspxhome.com/file/2023/8/101038_0s.png)
python 绘图模块matplotlib的使用简介
2023-08-09 19:09:01
![](https://img.aspxhome.com/file/2023/9/93809_0s.png)
python返回数组的索引实例
2023-11-01 04:48:17
python3 os进行嵌套操作的实例讲解
2022-11-01 09:49:35
![](https://img.aspxhome.com/file/2023/6/90816_0s.png)
Python 格式化输出_String Formatting_控制小数点位数的实例详解
2023-11-29 12:09:14
简单了解python中对象的取反运算符
2021-04-21 16:50:57
![](https://img.aspxhome.com/file/2023/5/82955_0s.jpg)
你知道吗实现炫酷可视化只要1行python代码
2022-06-10 13:36:16
![](https://img.aspxhome.com/file/2023/6/87686_0s.jpg)
Django实现快速分页的方法实例
2022-12-27 06:02:30
![](https://img.aspxhome.com/file/2023/3/72103_0s.jpg)
Pandas Matplotlib保存图形时坐标轴标签太长导致显示不全问题的解决
2023-07-22 20:03:09
![](https://img.aspxhome.com/file/2023/0/62940_0s.png)
MySQL数据库管理必备工具 phpMyAdmin 3.0
2008-12-10 14:12:00
![](https://img.aspxhome.com/file/UploadPic/200812/20081212115238456.jpg)
Python面向对象程序设计OOP入门教程【类,实例,继承,重载等】
2022-01-08 13:53:58
golang基于websocket实现的简易聊天室程序
2023-06-15 00:27:01