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

Selenium WebDriver无法在pure.app网站滚动的问题排查与解决

问题:pure.app页面滚动失效的Selenium自动化解决方案

问题背景

尝试用Python Selenium WebDriver实现pure.app自动点赞,核心需求是滚动页面加载新内容,但以下滚动方法均无效:

  • 使用driver.execute_script("window.scrollBy(0, 500)")滚动500像素
  • 用ActionChains(driver).send_keys(Keys.PAGE_DOWN).perform()模拟Page Down键
  • 自定义smooth_scroll()函数通过JS定时器渐进式滚动

代码运行无报错,但页面完全不滚动,内容保持静态。检查网站结构未发现明显的动态加载/无限滚动阻碍,页面看似支持滚动。

根本原因排查

  1. 滚动容器非全局window:多数现代网站的内容区域嵌套在独立<div>容器中,而非直接使用window滚动条,针对window的滚动操作自然无效。
  2. 反爬机制拦截:pure.app可能检测到自动化脚本,限制了滚动等交互操作。
  3. 元素未聚焦:滚动前未将焦点切换到内容区域,导致按键/JS操作无法触发滚动。

解决方案

1. 定位实际滚动容器并针对性滚动

通过浏览器开发者工具(Elements面板)找到带有overflow: auto或overflow: scroll样式的内容容器,针对该容器执行滚动操作。

2. 增强反反爬配置

添加真实User-Agent,避免被识别为自动化工具。

3. 优化滚动逻辑

替换全局滚动代码,改为直接操作滚动容器的scrollTop属性。

修改后的完整代码

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

# 配置Chrome选项,增强反反爬能力
chrome_options = Options()
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
# 添加真实User-Agent,模拟普通浏览器请求
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")

# ChromeDriver路径
chrome_driver_path = r'C:\chromedriver-win64\chromedriver.exe'

# 初始化驱动
service = Service(chrome_driver_path)
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.set_window_size(1920, 1080)

# 点赞函数
def find_and_click_like_button():
    like_button_selector = "div.sc-nukQN button.sc-koXPp.sc-bmzYkS:nth-child(3)"
    try:
        like_button = WebDriverWait(driver, 5).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, like_button_selector))
        )
        svg = like_button.find_element(By.TAG_NAME, "svg")
        if "fill: red" not in svg.get_attribute("outerHTML"):
            driver.execute_script("arguments[0].click();", like_button)
            print("点赞成功")
            return True
        else:
            print("已点赞,跳过")
            return False
    except Exception as e:
        print(f"点赞失败:{e}")
        return False

# 针对滚动容器的平滑滚动函数
def smooth_scroll_container(container_selector, distance):
    script = f"""
    var container = document.querySelector('{container_selector}');
    var current = 0;
    var interval = setInterval(function() {{
        container.scrollTop += 10;
        current += 10;
        if (current >= {distance}) clearInterval(interval);
    }}, 20);
    """
    driver.execute_script(script)

try:
    # Google登录流程
    driver.get("https://accounts.google.com/")
    email_input = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "input[type='email']"))
    )
    email_input.send_keys("XXXXXXXXXXXXXXXXXX")
    email_input.send_keys(Keys.RETURN)
    print("已输入邮箱")
    time.sleep(10)

    password_input = WebDriverWait(driver, 20).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "input[type='password']"))
    )
    password_input.send_keys("XXXXXXXXXXXXXXXX")
    print("已输入密码")
    next_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//button[@jsname='LgbsSe']//span[text()='Далее']"))
    )
    driver.execute_script("arguments[0].click();", next_button)
    print("已点击下一步")
    time.sleep(15)
    print("Google登录完成")

    # 进入pure.app并登录
    driver.get("https://pure.app/ru/")
    print("已进入pure.app")
    WebDriverWait(driver, 30).until(
        EC.presence_of_element_located((By.ID, "hamburger-btn"))
    )
    hamburger_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.ID, "hamburger-btn"))
    )
    driver.execute_script("arguments[0].click();", hamburger_button)
    print("已点击菜单按钮")
    sign_in_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.ID, "menu-sign-in"))
    )
    driver.execute_script("arguments[0].click();", sign_in_button)
    print("已点击SIGN IN")
    google_login_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'block uppercase h-[49px]') and contains(., 'С google')]"))
    )
    driver.execute_script("arguments[0].click();", google_login_button)
    print("已点击Google登录")
    print("pure.app登录完成")

    time.sleep(10)
    # 处理权限请求
    try:
        allow_button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//a[contains(@class, 'sc-dtInlm') and contains(@class, 'sc-kGCWdC')]//span[text()='РАЗРЕШИТЬ']"))
        )
        driver.execute_script("arguments[0].click();", allow_button)
        print("已点击授权按钮")
    except Exception as e:
        print(f"授权按钮点击失败:{e}")
    time.sleep(5)

    # 核心循环:点赞+滚动
    # 替换为pure.app实际的滚动容器选择器(需通过浏览器开发者工具确认)
    scroll_container_selector = "div[class*='feed-container']"
    # 等待滚动容器加载完成
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, scroll_container_selector))
    )
    # 聚焦到滚动容器,确保交互生效
    scroll_container = driver.find_element(By.CSS_SELECTOR, scroll_container_selector)
    driver.execute_script("arguments[0].focus();", scroll_container)

    while True:
        find_and_click_like_button()
        # 使用容器滚动替代全局滚动
        smooth_scroll_container(scroll_container_selector, 500)
        print("已滚动500像素")
        time.sleep(2)

        user_input = input("按Enter继续,输入'stop'停止:")
        if user_input.lower() == 'stop':
            print("已停止操作")
            break

except Exception as e:
    print(f"程序错误:{e}")

input("按Enter退出...")
driver.quit()

关键修改说明

  1. 添加真实User-Agent:模拟普通浏览器请求,降低被反爬识别的概率。
  2. 滚动容器定位:通过scroll_container_selector指定实际承载内容的容器,需根据pure.app页面结构调整(可通过浏览器开发者工具查看)。
  3. 容器聚焦:滚动前将焦点切换到容器,确保交互操作能触发滚动。
  4. 容器滚动逻辑:修改JS脚本直接操作容器的scrollTop属性,替代全局window滚动。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 00:28:14