我是靠谱客的博主 贪玩石头,最近开发中收集的这篇文章主要介绍selenium,Python3滚动到页面底部的几种解决方案,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

 

目录

  • location_once_scrolled_into_view
  • ActionChains
  • js方法scrollIntoView
  • js方法scrollBy
  • send_keys(Keys.END)模拟向页面发送空格键
  • 页面为ajax动态加载

 

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():
    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()

target 页面底部的元素

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

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():
    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()

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

js方法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():
    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()

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

js方法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():
    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()

time.sleep必须要加

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

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

#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():
    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()

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

页面为ajax动态加载

driver = webdriver.Chrome()

read_mores = driver.find_elements_by_xpath('//a[text()="加载更多"]')

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

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

标签: selenium, python3

最后

以上就是贪玩石头为你收集整理的selenium,Python3滚动到页面底部的几种解决方案的全部内容,希望文章能够帮你解决selenium,Python3滚动到页面底部的几种解决方案所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部