如何在 python 中使用硒“向下滚动”某些部分?

How to “scroll down” some part using selenium in python?

祝你一切顺利 我正在尝试制作一个简单的脚本,但我卡在那里 我正在尝试滚动列表以获取更多内容,但我无法向下滚动。任何人都知道这是怎么做到的。

这是我的代码:

import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from shutil import which
import time
import pandas as pd
import json
# from fake_useragent import UserAgent
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
chrome_path = which('chromedriver')
driver = webdriver.Chrome(executable_path=chrome_path)
driver.maximize_window()

driver.get('http://mapaescolar.murciaeduca.es/mapaescolar/#')

driver.find_element_by_xpath('//ul[@class="nav property-back-nav floating-box pull-right"]//button').click()
time.sleep(3)
driver.find_element_by_xpath('//button[@ng-click="openCloseFilters()"]').click()
time.sleep(3)
driver.find_element_by_xpath('//select[@title="Enseñanza"]/option[1]').click()



element = driver.find_element_by_xpath('//div[@id="container1"]')
driver.execute_script("return arguments[0].scrollIntoView(true);", element)

以及我要向下滚动的列表:

试试这个:

    SCROLL_PAUSE_TIME = 0.5

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height

或者简单地 select 你的元素在这种情况下 div 包含滚动视图并键入:

label.sendKeys(Keys.PAGE_DOWN);
# Use this line in a loop, accordingly how much screen to be scrolled down
# this just scrolls down to the height of browser screen, hence loop.

driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
# Now if the website needs time to load its data after scroll, add this in the loop..

time.sleep(5)

只需将脚本更改为arguments[0].scrollTop = arguments[0].scrollHeight。 您可以在某个带有超时的循环中调用该脚本以连续获取更多数据(即不断将 div 滚动到底部)

示例:

while True:
    time.sleep(1)
    driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", element)
    if no_new_data_available():
        break

因为滚动实际上是在一个元素内部,所有javascript命令都带有window。不管用。此外,该元素不可交互,因此 Key down 也不适合。 我建议使用 scrollTo javascript 执行器并设置一个变量,该变量将在你的循环中增加:

element = driver.find_element_by_xpath('//div[@id="container1"]')
time.sleep(10)

verical_ordinate = 100
for i in range(0, 50):
   print(verical_ordinate)
   driver.execute_script("arguments[0].scrollTop = arguments[1]", element, verical_ordinate)
   verical_ordinate += 100
   time.sleep(1)

我已经用 chrome 测试过,所以应该可以。

参考

https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop

在你的情况下,有一个项目列表,所以你可以按照这个方法:

for c in range(1, 12): # check the list start from 0 or 1 
    time.sleep(5)  # Give time to loading the information
    element = driver.find_element_by_xpath(f'//*[@id="grid-search-results"]/ul/li[{c}]') # variable c refer to next item
    driver.execute_script("arguments[0].scrollIntoView();", element)

element.location_once_scrolled_into_view

我在尝试访问不是因为需要向下滚动的元素时使用了它。