Selenium - 滚动条操作
移动到底部/顶部
import time from selenium import webdriver driver = webdriver.Chrome() driver.implicitly_wait(time_to_wait=10) driver.get("https://www.cnblogs.com/a438842265") # 获取当前的窗口对象 element = driver.find_element_by_tag_name("body") time.sleep(3) # 从顶部下拉到底部 driver.execute_script("arguments[0].scrollIntoView(false);",element) # 默认为true time.sleep(3) # driver.execute_script("window.scrollTo(0,document.body.scollHeight)") # 使用window对象 # 从底部拉到顶部 driver.execute_script("arguments[0].scrollIntoView(true);",element) time.sleep(3) driver.quit()
相对移动
# import time # # from selenium import webdriver # # driver = webdriver.Chrome() # driver.implicitly_wait(time_to_wait=10) # # driver.get("https://www.cnblogs.com/a438842265") # # # 获取当前的窗口对象 # element = driver.find_element_by_tag_name("body") # time.sleep(3) # # # 从顶部下拉到底部 # # driver.execute_script("arguments[0].scrollIntoView(false);",element) # 默认为true # time.sleep(3) # # driver.execute_script("window.scrollTo(0,document.body.scollHeight)") # 使用window对象 # # # 从底部拉到顶部 # driver.execute_script("arguments[0].scrollIntoView(true);",element) # # time.sleep(3) # driver.quit() import time from selenium import webdriver driver = webdriver.Chrome() driver.implicitly_wait(time_to_wait=10) driver.get("https://www.cnblogs.com/a438842265") time.sleep(3) driver.execute_script("window.scrollBy(0, 700)") # 相对移动,从当前位置移动700像素 time.sleep(3) driver.execute_script("window.scrollBy(0, 700)") # 相对移动,从当前位置移动700像素,即 上次移动的700 + 本次的700 = 1400像素 time.sleep(3) driver.quit()
绝对移动
import time from selenium import webdriver driver = webdriver.Chrome() driver.implicitly_wait(time_to_wait=10) driver.get("https://www.cnblogs.com/a438842265") time.sleep(3) driver.execute_script("window.scrollTo(0, 2200)") # 绝对移动,直接移动到2200像素位置 time.sleep(3) driver.execute_script("window.scrollTo(0, 200)") # 绝对移动,从上次的2200像素移动到200像素位置 time.sleep(3) driver.quit()