当前位置:网站首页>Web automation: how to realize browser window scrolling in 8.3 selenium
Web automation: how to realize browser window scrolling in 8.3 selenium
2022-04-22 08:05:00 【Flower test】
Window scrolling is not a web page operation , It belongs to window behavior . Realize window scrolling , Need to be in selenium Sent in js Instructions ( Reference resources :8.2 selenium How to send js Instructions )
Use scenarios :
1、 There are lazy loaded products : You don't load everything at once , Something must be done before something new comes out ( Such as : Scroll bar drop-down , Will load new things )
2、 Want to click or manipulate an element , But this element is not visible in the page . You need to pull the element into the visual range first , To click .
Introduce 3 A method of window scrolling :
Method 1 : Coordinate method
But we rarely use this method , Because generally, I don't know how many pixels of the element I'm positioning .
window.scrollTo(0,1000) # f(x,y) x: Abscissa ,y: Ordinate , Company :xp( Pixel point )
practice 1
from selenium import webdriver
import time
driver = webdriver.Chrome() # Initialize a Google browser
driver.maximize_window() # Maximize browser windows
driver.get("https://readhub.cn/topics") # Open a web page with lazy loading
time.sleep(5) # Wait for a while , Wait until the page is loaded js Code
# Method 1 : Coordinate method
driver.execute_script('window.scrollTo(0,1000)') # The abscissa does not change , Ordinate Scroll to 1000 Pixel point
time.sleep(2) # Wait for a while , Easy to see the effect of scrolling
driver.execute_script('window.scrollTo(0,2000)') # Scroll again
time.sleep(2) # Wait for a while , Easy to see the effect of scrolling
Method 2 : Scroll to the bottom of the window , Then locate the element to be found
window.scrollTo(0,document.body.scrollHeight) # document.body.scrollHeight The height of the current window
practice 2
from selenium import webdriver
import time
driver = webdriver.Chrome() # Initialize a Google browser
driver.maximize_window() # Maximize browser windows
driver.get("https://readhub.cn/topics") # Open a web page with lazy loading
time.sleep(5) # Wait for a while , Wait until the page is loaded js Code
# Method 2 : Scroll to the bottom of the window
# Pull to bottom , But with a new load, it will return to the two-thirds position ; After loading it, it jumps to the quarter position
driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
time.sleep(2) # Wait for a while , Easy to see the effect of scrolling
driver.execute_script('window.scrollTo(0,document.body.scrollHeight)') # Continue scrolling
Method 3 : Scroll elements to the visible range
scene : Sometimes elements are under comparison , You need to scroll down the page to see the element and click
document.getElementById(“id”).scrollIntoView()
Or write in two lines :
el = document.getElementById(“id”)
el.scrollIntoView()
practice 3
from selenium import webdriver
import time
driver = webdriver.Chrome() # Initialize a Google browser
driver.maximize_window() # Maximize browser windows
driver.get("https://www.baidu.com/s?ie=UTF-8&wd=%E8%8A%B1%E9%9D%99%E9%80%B8") # Open Baidu search “ Hua Jingyi ” The results page
time.sleep(5) # Wait for a while , Wait until the page is loaded js Code
# document.getElementsByClassName("pc")[1] # js Instructions : Baidu search results page , Locate the flipping element at the bottom of the page “2”
driver.execute_script('document.getElementsByClassName("pc")[1].scrollIntoView()')
# Or write in two lines
driver.execute_script('el = document.getElementsByClassName("pc")[1]')
driver.execute_script('el.scrollIntoView()')
Be careful : practice 3 There's a hole in it
getElementsByClassName Navigate to a set of elements , We need to index ( from 0 Start ) To get the element “2”

版权声明
本文为[Flower test]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220628391174.html
边栏推荐
- web问题定位实战:2.提示信息、字段校验
- mysql安装采坑
- Figure 2022 work plan of industrial Internet special working group
- The practical paper of Shiping information and data security compliance testing was selected into the Chinese core journals
- wordpress个人网站搭建
- 数据库原理课程设计——物资发放管理系统
- T-SQL: an interview question in Hospital Information Department
- Leaders of Hangzhou commercial password Application Association and their delegation visited Shiping for information exchange
- Plain CSRF vulnerability
- 初级测试:普通 VS 优秀
猜你喜欢

LoadRunner-性能测试工具

China network security industry panorama of ccsip 2021 listed by Shiping information

Understanding of transformer mechanism

网页导入3D模型-obj/mtl文件

Raspberry pie Lite: install the latest version of discuz

如何保护个人信息安全?@每个人,一定不要错过这份干货分享

Software development specification

技术解惑 | PLC是如何执行中断?又是如何去应用的?

世平信息数据安全合规检测实践论文入选中文核心期刊

图解《工业互联网专项工作组2022年工作计划》
随机推荐
Shiping information participated in the enterprise roadshow of Hengyang "Chuanshan forum", talked about data security and helped collaborative innovation
世平信息参与衡阳市“船山论坛”企业路演,畅谈数据安全,助力协同创新
配置表及页面信息 自动生成CURD操作页面
世平信息上榜《2021年中国网络安全市场全景图》
杭州市商用密码应用协会领导一行莅临世平信息参观交流
The difference between synchronized and reentrantlock
落实数据合规,保障数据安全
Shiping information has successfully passed CMMI Level 3 certification
Flash data model migration error
PLC项目调试七步法
WordPress personal website construction
Plain CSRF vulnerability
Principle and application of SSRF and xxE attack
Raspberry Pie: simulating iSCSI storage
About XSS Cross Station
【世平信息】刻录内容保密检查与合规管控解决方案
Why do I value document naming so much?
专注数据安全,世平信息上榜中国网络安全行业全景图六大细分领域
China network security industry panorama of ccsip 2021 listed by Shiping information
Shiping information has successfully passed the evaluation of "capability verification of data security products" of China Academy of communications and communications