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

浏览器能否检测Puppeteer驱动?如何规避检测模拟人类行为?

Puppeteer反检测与模拟人类行为指南

一、浏览器检测Puppeteer的常见方式

  • 特征标识暴露:Puppeteer启动的浏览器会携带专属标识,比如navigator.webdriver默认返回true(正常浏览器为undefined);无头模式下window.chrome对象的属性结构会和原生浏览器存在差异。
  • 行为模式异常:程序化操作的节奏过于规整——比如点击、输入速度均匀无停顿,滚动、跳转动作完全机械,不符合人类操作的随机性和停顿习惯。
  • 启动参数痕迹:Puppeteer启动浏览器时会附带--remote-debugging-port等专属命令行参数,部分网站会通过解析启动参数识别自动化工具。
  • 指纹不一致:Canvas、WebGL等浏览器指纹会和原生浏览器存在差异,因为Puppeteer的渲染环境与真实浏览器并非完全一致。

二、规避检测与模拟人类行为的方法

1. 抹除自动化特征标识

  • 启动浏览器时添加--disable-blink-features=AutomationControlled参数,直接屏蔽navigator.webdriver等自动化标识。
  • 移除默认的自动化提示参数,在启动配置中添加ignoreDefaultArgs: ['--enable-automation']。
  • 手动补充浏览器对象缺失的属性,模拟真实Chrome环境:
    await page.evaluate(() => {
        Object.defineProperty(navigator, 'webdriver', {get: () => undefined});
        window.chrome = {
            runtime: {},
            // 补充更多真实Chrome的属性结构
        };
    });
    

2. 优化浏览器启动参数

使用更贴近原生浏览器的启动配置,减少异常特征:

const launchOptions = {
    headless: false,
    executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
    args: [
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--disable-blink-features=AutomationControlled',
        '--start-maximized', // 最大化窗口,模拟人类打开浏览器的状态
        '--user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"' // 设置真实用户代理
    ],
    ignoreDefaultArgs: ['--enable-automation']
};

3. 模拟人类操作行为

  • 添加随机延迟:在页面跳转、元素操作前加入随机等待时间,模拟人类思考停顿:
    await page.waitForTimeout(Math.random() * 1500 + 500); // 500-2000ms随机延迟
    
  • 逐字符随机输入:拆分文本内容逐个输入,每个字符间隔随机时间,避免一次性输入的机械感:
    const typeWithHumanDelay = async (page, selector, text) => {
        await page.focus(selector);
        for (const char of text) {
            await page.keyboard.type(char);
            await page.waitForTimeout(Math.random() * 200 + 50); // 50-250ms间隔
        }
    };
    
  • 随机鼠标移动与滚动:使用page.mouse.move()模拟鼠标随机游走,或执行原生滚动脚本模拟人类浏览时的滚动节奏:
    // 随机滚动页面
    await page.evaluate(() => {
        window.scrollBy(0, Math.random() * 300 + 100);
    });
    

4. 借助第三方插件简化反检测流程

使用puppeteer-extra搭配stealth插件,自动处理大部分常见检测点:

const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');

puppeteer.use(StealthPlugin());

const testRun = async () => {
    const launchOptions = {
        headless: false,
        executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
        args: ['--start-maximized'],
        ignoreDefaultArgs: ['--enable-automation']
    };
    
    const browser = await puppeteer.launch(launchOptions);
    const page = await browser.newPage();

    // 模拟人类延迟后跳转
    await page.waitForTimeout(Math.random() * 1500 + 1000);
    await page.goto("https://www.google.com/", {
        waitUntil: "networkidle2",
        timeout: 0,
    });

    // 模拟人类搜索操作
    await page.waitForTimeout(Math.random() * 1000 + 500);
    await typeWithHumanDelay(page, 'input[name="q"]', 'Puppeteer反检测');
    await page.waitForTimeout(Math.random() * 800 + 300);
    await page.keyboard.press('Enter');
};

const typeWithHumanDelay = async (page, selector, text) => {
    await page.focus(selector);
    for (const char of text) {
        await page.keyboard.type(char);
        await page.waitForTimeout(Math.random() * 200 + 50);
    }
};

testRun();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 16:26:06