如果你還想從頭學起Selenium,可以看看這個系列的文章哦!
https://www.cnblogs.com/poloyy/category/1680176.html
其次,如果你不懂前端基礎知識,需要自己去補充哦,博主暫時沒有總結(雖然我也會,所以我學selenium就不用復習前端了哈哈哈...)
如何通過selenium控制瀏覽器滾動條呢?
- selenium沒有提供原生的滾動頁面方法,所以我們得通過最原始的JS來控制
- 原理:通過 driver.execute_script() 執行js代碼,達到目的
方式一:scrollBy(x,y)
driver.execute_script("window.scrollBy(0,1000)")
x:必傳,正數則向右滑動的像素值,負數則向左滑動的像素值
y:必傳,正數則向下滑動的像素值,負數則向上滑動的像素值
方式二:scrollTo(x,y)
driver.execute_script("window.scrollTo(0,1000)")
x:必傳,正數則向右滑動的像素值,負數則向左滑動的像素值
y:必傳,正數則向下滑動的像素值,負數則向上滑動的像素值
方式三:document.documentElement.scrollTop
作用一:獲取當前滾動高度
# 獲取當前滾動高度 scrolTop = driver.execute_script("document.documentElement.scrollTop")
作用二:設置滾動高度
# 設置滾動高度 driver.execute_script("document.documentElement.scrollTop=1000")