需要借助js
方法1:
execute_script("window.scrollBy(x,y)") # 必要参数x,y,x指横向移动的像素,y指竖向
方法2:
driver.execute_script("document.documentElement.scrollTop=10000") # scrollTop=0指的是滑动到顶部,10000指滑动到底部
打开微云首页,点击[微云服务协议],分别向下滑动一点和滑动到底。注意打开协议后,需要匹配到新的window_handle才能操作滑动,不然是会无法滑动这一页的。
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait import time import os if __name__ == "__main__": firefox_driver = webdriver.Firefox() firefox_driver.get("https://www.weiyun.com/") first_window = firefox_driver.current_window_handle print(firefox_driver) firefox_driver.find_element_by_link_text("微云服务协议").click() time.sleep(1) window_handles = firefox_driver.window_handles for current_window in window_handles: if current_window != first_window: firefox_driver.switch_to.window(current_window) print(current_window) else: pass firefox_driver.execute_script("window.scrollBy(0,100)") time.sleep(2) firefox_driver.execute_script("document.documentElement.scrollTop=10000") time.sleep(2) firefox_driver.execute_script("window.scrollBy(0,100)") time.sleep(1) firefox_driver.quit()
原文:https://www.cnblogs.com/Cc905/p/12883905.html