When retrieving pages using Selenium, there are many times when you need to drag the scroll bar to the bottom of the page. Several methods are summarized below.

location_once_scrolled_into_view

#coding=utf-8

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains


browser=webdriver.Chrome("G:/dj/chromedriver.exe")
wait=WebDriverWait(browser,10)
browser.set_window_size(1400.900)
import time

def search(a):
    try:
        browser.get("https://www.taobao.com") target=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"body > div:nth-child(29)")))
        target.location_once_scrolled_into_view
    except TimeoutException:
        search()
search()
Copy the code

Target element at the bottom of the page

If the page is dynamically rendered with Ajax, the height of the page changes over time, so this method will probably not jump to the bottom of the page

ActionChains

#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains

browser=webdriver.Chrome("G:/dj/chromedriver.exe")
wait=WebDriverWait(browser,10)
browser.set_window_size(1400.900)
import time

def search(a):
    try:
        browser.get("https://www.taobao.com")
        total=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"body > div:nth-child(29)")))
        target = browser.find_element_by_css_selector('body > div:nth-child(29)')
        actions = ActionChains(browser)
        actions.move_to_element(target)
        actions.perform()
    except TimeoutException:
        search()

search()
Copy the code

If the page is dynamically rendered with Ajax, the height of the page changes over time, so this method will probably not jump to the bottom of the page

Js method scrollIntoView

#coding=utf-8

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains


browser=webdriver.Chrome("G:/dj/chromedriver.exe")
wait=WebDriverWait(browser,10)
browser.set_window_size(1400.900)
import time

def search(a):
    try:
        browser.get("https://www.taobao.com") total=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"body > div:nth-child(29)")))

        browser.execute_script('arguments[0].scrollIntoView(true); ', total)

    except TimeoutException:
        search()
search()
Copy the code

If the page is dynamically rendered with Ajax, the height of the page changes over time, so this method will probably not jump to the bottom of the page

Js method scrollBy

#coding=utf-8

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains


browser=webdriver.Chrome("G:/dj/chromedriver.exe")
wait=WebDriverWait(browser,10)
browser.set_window_size(1400.900)
import time

def search(a):
    try:
        browser.get("https://www.taobao.com")

        total=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"body > div:nth-child(29)")))

        for i in range(15):
            browser.execute_script("window.scrollBy(0, 1000)")
            time.sleep(1)

    except TimeoutException:
        search()

search()
Copy the code

Time. Sleep must be added

A page suitable for dynamic ajax rendering, with multiple jumps to the bottom of the page

Send_keys (keys.end) simulates sending space bars to the page

#coding=utf-8

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains


browser=webdriver.Chrome("G:/dj/chromedriver.exe")
wait=WebDriverWait(browser,10)
browser.set_window_size(1400.900)
import time

def search(a):
    try:
        browser.get("https://www.taobao.com")

        total=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"body > div:nth-child(29)")))

        for i in range(5):
             browser.find_element_by_tag_name('body').send_keys(Keys.END)
             time.sleep(1)
    except TimeoutException:
        search()
search()

Copy the code

A page suitable for dynamic ajax rendering, with multiple jumps to the bottom of the page

Pages are loaded dynamically with Ajax

driver = webdriver.Chrome()

read_mores = driver.find_elements_by_xpath('//a[text()=" load more "]')

for read_more in read_mores:
    driver.execute_script("arguments[0].scrollIntoView();", read_more)
    driver.execute_script("$(arguments[0]).click();", read_more)
Copy the code

Original text: rumenz.com/rumenbiji/p…