Python 用于向下滚动不可滚动页面的脚本

Python script to scroll down non scroll-able page

我有一个 Airtable table 我偶尔回顾一下并尝试使用 selenium 创建一个 Python 脚本来向下滚动整个页面直到它到达末尾。这是代码,但我无法向下滚动。我没有收到任何错误,但它似乎没有连接到要滚动的页面。任何帮助表示赞赏。谢谢

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep

url = 'https://airtable.com/embed/shrqYt5kSqMzHV9R5/tbl8c8kanuNB6bPYr?backgroundColor=green&viewControls=on'

driver = webdriver.Chrome()
driver.get(url)
driver.fullscreen_window()
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//html')))

scroll_pause_time = 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
    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

您无法滚动 table 的原因是您的页面反 scroll-able。您可以通过简单地尝试手动滚动来检查这一点。要加载您的页面,您必须通过单击来拖动垂直滚动条。为此,我们可以使用 ActionChains class 的 drag_and_drop_by_offset 方法,如下所示:

# After your page is loaded
page_hight = driver.get_window_size()['height'] #Get page height
scroll_bar = driver.find_element_by_xpath("//div[contains(@class,'antiscroll-scrollbar-vertical')]")
ActionChains(driver).drag_and_drop_by_offset(scroll_bar, 0, page_hight-160).click().perform() #Substracted 160 fro page height to compensate differnec between window and screen height

输出