selenium,Python3滚动到页面底部的几种解决方案

在用selenium获取页面时,很多时候需要将滚动条拖到页面底部,下面总结了几种方法.

location_once_scrolled_into_view

  1. #coding=utf-8
  2. from selenium import webdriver
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.common.keys import Keys
  5. from selenium.webdriver.support import expected_conditions as EC
  6. from selenium.webdriver.support.ui import WebDriverWait
  7. from selenium.common.exceptions import TimeoutException
  8. from selenium.webdriver.common.action_chains import ActionChains
  9. browser=webdriver.Chrome("G:/dj/chromedriver.exe")
  10. wait=WebDriverWait(browser,10)
  11. browser.set_window_size(1400,900)
  12. import time
  13. def search():
  14. try:
  15. browser.get("https://www.taobao.com") target=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"body > div:nth-child(29)")))
  16. target.location_once_scrolled_into_view
  17. except TimeoutException:
  18. search()
  19. search()

target 页面底部的元素

如果页面是ajax动态渲染的,页面的高度随时变化的,所以这个方法很有可能跳不到页面底部

ActionChains

  1. #coding=utf-8
  2. from selenium import webdriver
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.common.keys import Keys
  5. from selenium.webdriver.support import expected_conditions as EC
  6. from selenium.webdriver.support.ui import WebDriverWait
  7. from selenium.common.exceptions import TimeoutException
  8. from selenium.webdriver.common.action_chains import ActionChains
  9. browser=webdriver.Chrome("G:/dj/chromedriver.exe")
  10. wait=WebDriverWait(browser,10)
  11. browser.set_window_size(1400,900)
  12. import time
  13. def search():
  14. try:
  15. browser.get("https://www.taobao.com")
  16. total=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"body > div:nth-child(29)")))
  17. target = browser.find_element_by_css_selector('body > div:nth-child(29)')
  18. actions = ActionChains(browser)
  19. actions.move_to_element(target)
  20. actions.perform()
  21. except TimeoutException:
  22. search()
  23. search()

如果页面是ajax动态渲染的,页面的高度随时变化的,所以这个方法很有可能跳不到页面底部

js方法scrollIntoView

  1. #coding=utf-8
  2. from selenium import webdriver
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.common.keys import Keys
  5. from selenium.webdriver.support import expected_conditions as EC
  6. from selenium.webdriver.support.ui import WebDriverWait
  7. from selenium.common.exceptions import TimeoutException
  8. from selenium.webdriver.common.action_chains import ActionChains
  9. browser=webdriver.Chrome("G:/dj/chromedriver.exe")
  10. wait=WebDriverWait(browser,10)
  11. browser.set_window_size(1400,900)
  12. import time
  13. def search():
  14. try:
  15. browser.get("https://www.taobao.com") total=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"body > div:nth-child(29)")))
  16. browser.execute_script('arguments[0].scrollIntoView(true);', total)
  17. except TimeoutException:
  18. search()
  19. search()

如果页面是ajax动态渲染的,页面的高度随时变化的,所以这个方法很有可能跳不到页面底部

js方法scrollBy

  1. #coding=utf-8
  2. from selenium import webdriver
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.common.keys import Keys
  5. from selenium.webdriver.support import expected_conditions as EC
  6. from selenium.webdriver.support.ui import WebDriverWait
  7. from selenium.common.exceptions import TimeoutException
  8. from selenium.webdriver.common.action_chains import ActionChains
  9. browser=webdriver.Chrome("G:/dj/chromedriver.exe")
  10. wait=WebDriverWait(browser,10)
  11. browser.set_window_size(1400,900)
  12. import time
  13. def search():
  14. try:
  15. browser.get("https://www.taobao.com")
  16. total=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"body > div:nth-child(29)")))
  17. for i in range(15):
  18. browser.execute_script("window.scrollBy(0, 1000)")
  19. time.sleep(1)
  20. except TimeoutException:
  21. search()
  22. search()

time.sleep必须要加

适合ajax动态渲染的页面,分多次跳到页面底部

send_keys(Keys.END)模拟向页面发送空格键

  1. #coding=utf-8
  2. from selenium import webdriver
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.common.keys import Keys
  5. from selenium.webdriver.support import expected_conditions as EC
  6. from selenium.webdriver.support.ui import WebDriverWait
  7. from selenium.common.exceptions import TimeoutException
  8. from selenium.webdriver.common.action_chains import ActionChains
  9. browser=webdriver.Chrome("G:/dj/chromedriver.exe")
  10. wait=WebDriverWait(browser,10)
  11. browser.set_window_size(1400,900)
  12. import time
  13. def search():
  14. try:
  15. browser.get("https://www.taobao.com")
  16. total=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"body > div:nth-child(29)")))
  17. for i in range(5):
  18. browser.find_element_by_tag_name('body').send_keys(Keys.END)
  19. time.sleep(1)
  20. except TimeoutException:
  21. search()
  22. search()

适合ajax动态渲染的页面,分多次跳到页面底部

页面为ajax动态加载

  1. driver = webdriver.Chrome()
  2. read_mores = driver.find_elements_by_xpath('//a[text()="加载更多"]')
  3. for read_more in read_mores:
  4. driver.execute_script("arguments[0].scrollIntoView();", read_more)
  5. driver.execute_script("$(arguments[0]).click();", read_more)

原文: https://rumenz.com/rumenbiji/python3-selenium-scrollToBottom.html

返回笔记列表
入门小站