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

Selenium无法在Gumroad发现页滚动的问题求助

问题:Selenium无法在Gumroad搜索发现页实现滚动加载

我是编程新手(这不是作业题)。我尝试用Selenium实现页面无限滚动,脚本在其他网站都能正常运行,但在Gumroad搜索后的发现页无法生效。试过各种方法和网上方案都没解决,问题仅出现在搜索后的发现板块。

附上测试代码:

from selenium.webdriver.common.by import By

website = (r'https://discover.gumroad.com/?query=3d')
driver = webdriver.Chrome('C:\Program Files (x86)\chromedriver.exe')
driver.get(website)

driver.execute_script("window.scrollTo(0, 5000)") 

可行解决方案

Gumroad发现页的滚动加载逻辑不依赖全局窗口滚动,而是需要针对商品列表容器操作,同时要循环等待内容加载,以下是两种有效方案:

方案1:定位内容容器滚动到底部

直接操作商品列表所在的容器,触发加载逻辑:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

website = 'https://discover.gumroad.com/?query=3d'
driver = webdriver.Chrome('C:\Program Files (x86)\chromedriver.exe')
driver.get(website)

# 等待核心内容容器加载完成
wait = WebDriverWait(driver, 10)
content_container = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div[data-test-id="discover-results"]')))

# 循环滚动加载直到没有新内容
last_height = driver.execute_script("return arguments[0].scrollHeight", content_container)
while True:
    # 滚动到容器底部
    driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", content_container)
    # 等待新内容加载(可根据网络情况调整时长)
    time.sleep(2)
    # 对比高度判断是否加载完成
    new_height = driver.execute_script("return arguments[0].scrollHeight", content_container)
    if new_height == last_height:
        break
    last_height = new_height

方案2:模拟鼠标滚轮滚动

模拟用户手动滚动的行为,逐步触发加载:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time

website = 'https://discover.gumroad.com/?query=3d'
driver = webdriver.Chrome('C:\Program Files (x86)\chromedriver.exe')
driver.get(website)

# 按需调整滚动次数,每次滚动1000像素
for _ in range(10):
    ActionChains(driver).scroll_by_amount(0, 1000).perform()
    time.sleep(2)

注意事项

  • 不要设置过短的等待间隔,避免触发Gumroad的反爬限制
  • 优先用WebDriverWait等待元素加载,替代固定time.sleep让代码更稳定
  • 如果容器选择器失效,可通过浏览器开发者工具重新定位商品列表的父容器

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 01:01:11