selenium中沒有直接控制滾動條的方法,能夠使用方法:execute_script(),能夠直接執行js的腳本。web
1、豎向滾動條控制,三種方法總有一款適合你。windows
1.滾動條拉到最底:ui
js="var q=document.documentElement.scrollTop=10000"spa
driver.execute_script(js)3d
2.滾動條拉到最底: code
js="var q=document.getElementById('id').scrollTop=10000"blog
driver.execute_script(js)ip
這裏的id爲滾動條的id,滾動條沒有id的網頁此方法不適用。ci
3.滾動條拉到最底:element
js = "var q=document.body.scrollTop=10000"
driver.execute_script(js)
能夠修改scrollTop 的值來定位右側滾動條的位置,0是最最頂部,10000是最底部。
在百度中搜索selenium,拖動滾動條至最底端:
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from time import sleep driver = webdriver.Chrome() driver.get("https://www.baidu.com") driver.maximize_window() # 搜索 selenium driver.find_element_by_id("kw").send_keys("selenium") driver.find_element_by_id("su").click() # 等待元素可見 WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="5"]/h3/a'))) # 將滾動條下拉至最低 js="var q=document.documentElement.scrollTop=10000" driver.execute_script(js)
2、橫向滾動條控制
1.經過座標控制橫向和縱向滾動條 scrollTo(x, y)
js = "window.scrollTo(100,400);"
driver.execute_script(js)
3、將目標元素拖動到頁面可見區域
1.可見元素與頁面「底端」對齊,scrollIntoView(false)
2.可見元素與頁面「頂端」對齊,scrollIntoView()
在百度中搜索selenium,拖動元素至頁面底端,便於點擊操做(由於在百度中將元素拖動至頁面頂端時,會被百度網頁自帶的表頭遮擋住,點擊時會報錯)
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from time import sleep driver = webdriver.Chrome() driver.get("https://www.baidu.com") driver.maximize_window() # 搜索 selenium driver.find_element_by_id("kw").send_keys("selenium") driver.find_element_by_id("su").click() # 等待元素可見 WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="5"]/h3/a'))) # 1.先找到元素 element = driver.find_element_by_xpath('//*[@id="5"]/h3/a') # 2.利用js將元素拖動到可見區域 driver.execute_script("arguments[0].scrollIntoView(false);",element) #可見元素與頁面「底端」對齊
執行結果以下:
:
3、滾動頁面到最底部
方法:window.scrollTo
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from time import sleep driver = webdriver.Chrome() driver.get("https://www.baidu.com") driver.implicitly_wait(20) driver.maximize_window() # 搜索 selenium driver.find_element_by_id("kw").send_keys("selenium") driver.find_element_by_id("su").click() # 等待元素可見 WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="5"]/h3/a'))) # 1.操做元素 driver.find_element_by_xpath('//*[@id="5"]/h3/a').click() # 2.等待新窗口打開 WebDriverWait(driver,10).until(EC.new_window_is_opened(windows)) # 獲取當前全部窗口句柄 windows = driver.window_handles # 3.切換到最後一個窗口 driver.switch_to.window(windows[-1]) # 滾動頁面到最底部 driver.execute_script("window.scrollTo(0,document.body.scrollHeight)") sleep(5) driver.quit()
==================================================================================
尊重原創,若需轉載請註明出處:https://www.cnblogs.com/lizhe860/
==================================================================================