学习笔记~
from selenium import webdriver
from selenium.webdriver.common import keys
from selenium.webdriver.common.by import By
import time
from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome()
# ===============模拟操作鼠标滑轮====================
driver.get('https://baike.baidu.com/')
# 1.滑动至页面底部
js = "window.scrollTo(0,document.body.scrollHeight)" # 前端固定的
driver.execute_script(js)
time.sleep(1)
# 2.滑动至顶部
js = "window.scrollTo(0,-document.body.scrollHeight)"
driver.execute_script(js)
time.sleep(1)
# 3.滑动至具体位置
js = "window.scrollBy(0,500)" # 向下滑动500个像素 传入x,y坐标
driver.execute_script(js)
time.sleep(1)
# 4.滑动至目标元素可见
el1 = driver.find_element(By.XPATH, ".//div[text()='特色词条']")
driver.execute_script("arguments[0].scrollIntoView();", el1) # 向下滚动至 - 元素可见
time.sleep(1)
el2 = driver.find_element(By.XPATH, ".//a[text()='首页']")
driver.execute_script("arguments[0].scrollIntoView(false);", el2) # 向上滚动至 - 元素可见
time.sleep(2)