You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Selenium滚动Telegram群组页面?常规方法均失败

解决Telegram网页版群组页面Selenium滚动失效问题

核心原因

Telegram网页版的聊天内容区域是独立的滚动容器,而非全局window或body,所以直接调用window.scroll类命令无法生效。

可行解决方案

1. 定位滚动容器并修改scrollTop属性

先找到聊天区域的滚动容器(通常是带有特定类名或属性的div,比如div.im_history_scrollable_wrap或div[data-scrollable="true"]),再通过JS修改其scrollTop值:

# 定位滚动容器元素
scroll_container = driver.find_element(By.CSS_SELECTOR, 'div.im_history_scrollable_wrap')

# 向下滚动(增加scrollTop值)
driver.execute_script("arguments[0].scrollTop += 500;", scroll_container)

# 向上滚动(减少scrollTop值)
driver.execute_script("arguments[0].scrollTop -= 500;", scroll_container)

# 滚动到顶部
driver.execute_script("arguments[0].scrollTop = 0;", scroll_container)

# 滚动到底部
driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight;", scroll_container)

2. 模拟鼠标滚轮操作

通过ActionChains模拟鼠标在滚动容器上的滚轮动作,更贴近真实用户操作,不易被拦截:

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions.wheel_input import ScrollOrigin

scroll_container = driver.find_element(By.CSS_SELECTOR, 'div.im_history_scrollable_wrap')

# 向下滚动
scroll_origin = ScrollOrigin.from_element(scroll_container)
ActionChains(driver)\
    .scroll_from_origin(scroll_origin, 0, 500)\
    .perform()

# 向上滚动
ActionChains(driver)\
    .scroll_from_origin(scroll_origin, 0, -500)\
    .perform()

3. 聚焦容器后模拟键盘按键

先让滚动容器获得焦点,再发送PageUp/PageDown等按键:

from selenium.webdriver.common.keys import Keys

scroll_container = driver.find_element(By.CSS_SELECTOR, 'div.im_history_scrollable_wrap')
scroll_container.click()  # 聚焦滚动容器
scroll_container.send_keys(Keys.PAGE_DOWN)  # 向下翻页
scroll_container.send_keys(Keys.PAGE_UP)    # 向上翻页

注意事项

  • 滚动容器的选择器可能随Telegram网页版更新变化,建议用浏览器开发者工具(F12)实时定位当前容器元素。
  • 操作前添加适当等待(time.sleep()或显式等待),避免页面未加载完成导致元素定位失败。

内容的提问来源于stack exchange,提问作者RaymanSix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 03:23:25