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

Java+Selenium实现类人类鼠标滚动的代码需求(新手求助)

Simulate Human-Like Scrolling with Java & Selenium

Hey there! Congrats on diving into coding just 3 days ago—love that you're building something out of pure hobby! The default JavascriptExecutor scroll can feel pretty robotic, so let's tweak it to mimic how real people scroll: we'll add random pauses, variable scroll distances, and even occasional small backtracks to make it look natural.

Here's a complete, commented code snippet you can use:

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import java.util.Random;

public class HumanScroll {
    public static void simulateHumanScroll(WebDriver driver, int totalScrolls) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        Random random = new Random();
        
        for (int i = 0; i < totalScrolls; i++) {
            // Random scroll distance: between 100-500px (humans don't scroll fixed amounts)
            int scrollDistance = random.nextInt(401) + 100;
            
            // Random pause before scrolling: 0.5-2 seconds (simulate reading content)
            try {
                Thread.sleep(random.nextInt(1501) + 500);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            
            // Execute the scroll down
            js.executeScript("window.scrollBy(0, " + scrollDistance + ");");
            
            // 10% chance to scroll back a little (humans sometimes re-read parts)
            if (random.nextDouble() < 0.1) {
                int backScroll = random.nextInt(101) + 50;
                try {
                    Thread.sleep(300); // Short pause before backtracking
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                js.executeScript("window.scrollBy(0, -" + backScroll + ");");
            }
        }
    }

    // To use it in your code:
    // simulateHumanScroll(driver, 10); // Scroll ~10 times with natural behavior
}

Key details that make this feel human:

  • Random scroll distances: Instead of scrolling the same amount every time, we pick between 100-500px—just like how people scroll more or less depending on what they're reading.
  • Variable pauses: We wait 0.5-2 seconds before each scroll to simulate stopping to look at content.
  • Occasional backtracking: 10% of the time, we scroll up 50-150px—this mimics when someone realizes they missed something and scrolls back a bit.
  • Smooth incremental scrolling: By doing multiple small scrolls instead of one big jump, it matches how humans move the scroll wheel or drag the scroll bar.

Quick tips for even more realism:

  • Adjust the totalScrolls parameter based on how long you want the scrolling to last.
  • Tweak the scroll distance and pause ranges if you want faster/slower scrolling (e.g., 50-300px for quicker skimming).
  • If you want to scroll until the bottom of the page, you can add a check for document.body.scrollHeight instead of using a fixed number of scrolls.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:22:34