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

Posted 入门小站

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了selenium,Python3滚动到页面底部的几种解决方案相关的知识,希望对你有一定的参考价值。

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

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滚动到页面底部的几种解决方案的主要内容,如果未能解决你的问题,请参考以下文章

BDD测试之selenium控制滚动条

使用 Selenium 在 Python 中滚动到页面顶部

在 Python 中使用 Selenium 滚动模式窗口

selenium 常见操作,js操作-将元素滚动到页面可见区域

在 Python 中使用 PhantomJS 向下滚动到无限页面的底部

selenium操作下拉滚动条的几种方法