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

如何解决puppeteer.use不是函数问题及无头模式运行异常

问题与完整解决方案

问题背景

无法以headless: true模式运行Puppeteer程序,引入puppeteer-extra的Stealth和Adblocker插件后,运行时抛出错误:

Uncaught TypeError TypeError: puppeteer.use is not a function
at <anonymous> (undefined:3:11)
at Module._compile (undefined:1159:14)
at Module._extensions..js (undefined:1213:10)
at Module.load (undefined:1037:32)
at Module._load (undefined:878:12)
at executeUserEntryPoint (undefined:81:12)
at <anonymous> (undefined:23:47)

修复该错误后,又出现程序无法被目标网站识别的问题。

错误原因

puppeteer.use()是puppeteer-extra专属的扩展方法,原生puppeteer并没有这个方法。你代码里直接导入了原生puppeteer,导致调用use时触发类型错误。

完整修复步骤

1. 替换puppeteer导入源

从puppeteer-extra导入核心实例,而非原生puppeteer,这样才能加载插件扩展功能。

2. 优化headless模式配置

新版Chrome的旧headless: true模式极易被反爬检测,改用headless: "new"模式;同时添加启动参数模拟真实浏览器环境,移除自动化标识。

3. 简化元素选择器(可选)

原代码使用的超长类名选择器会因网站更新失效,改用文本选择器、属性选择器提升稳定性。

修正后的完整代码

// 从puppeteer-extra导入核心实例,替代原生puppeteer
const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
const AdblockerPlugin = require('puppeteer-extra-plugin-adblocker');

// 注册插件
puppeteer.use(StealthPlugin());
puppeteer.use(AdblockerPlugin({ blockTrackers: true }));

const product_url = "https://www.zalando.se/nike-sportswear-air-flight-lite-mid-hoega-sneakers-whiteblack-ni112n02z-a11.html";
const cart = "https://www.zalando.se/cart";

async function givePage() {
  const browser = await puppeteer.launch({
    headless: "new", // 使用新版headless模式,规避检测
    args: [
      '--no-sandbox',
      '--disable-setuid-sandbox',
      '--disable-blink-features=AutomationControlled',
      '--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36'
    ],
    ignoreDefaultArgs: ['--enable-automation'] // 移除自动化标识
  });
  const page = await browser.newPage();
  
  // 额外隐藏自动化特征
  await page.evaluateOnNewDocument(() => {
    Object.defineProperty(navigator, 'webdriver', {
      get: () => undefined,
    });
  });
  
  return page;
}

async function addToCart(page) {
  // 访问商品页,等待网络稳定
  await page.goto(product_url, { waitUntil: 'networkidle2' });
  
  // 处理Cookie弹窗
  try {
    await page.waitForSelector("button.uc-btn-primary", { timeout: 5000 });
    await page.click("button.uc-btn-primary");
  } catch (e) {
    console.log("无Cookie弹窗或加载超时");
  }
  
  // 点击"Handla"按钮(用文本选择器简化)
  try {
    await page.waitForSelector("button:has-text('Handla')", { timeout: 10000 });
    await page.click("button:has-text('Handla')");
  } catch (e) {
    console.log("添加购物车按钮未找到或超时");
    return;
  }
  
  // 选择EU41尺码(用属性文本匹配)
  try {
    await page.waitForSelector("div[aria-label*='EU 41']", { timeout: 5000 });
    await page.click("div[aria-label*='EU 41']");
    console.log("已选择尺码EU 41");
  } catch (e) {
    console.log("尺码选择失败");
    return;
  }
  
  await new Promise(r => setTimeout(r, 1000));
  
  // 跳转购物车
  await page.goto(cart, { waitUntil: 'networkidle2' });
  
  // 点击结算按钮
  try {
    await page.waitForSelector("button:has-text('Gå till checkout')", { timeout: 5000 });
    await page.click("button:has-text('Gå till checkout')");
  } catch (e) {
    console.log("结算按钮未找到");
    return;
  }
  
  await new Promise(r => setTimeout(r, 1000));
  
  // 登录操作
  try {
    const email = await page.waitForSelector("#login.email", { timeout: 5000 });
    await email.type('marc.ehadsadwa@gmail.com');
    await new Promise(r => setTimeout(r, 500));
    
    const password = await page.waitForSelector("#login.secret", { timeout: 5000 });
    await password.type('Bradasdwad');
    await new Promise(r => setTimeout(r, 1000));
    
    await page.click("button:has-text('Logga in')");
    console.log("登录操作已执行");
  } catch (e) {
    console.log("登录步骤失败");
  }
  
  // 后续选择取货点的逻辑可在此补充
}

async function checkout() {
  const page = await givePage();
  await addToCart(page);
  // 执行完记得关闭浏览器,避免资源泄漏
  // await page.browser().close();
}

checkout();

关键配置说明

  • headless: "new":Puppeteer 19+对应Chrome 110+,新版headless模式与真实浏览器行为更接近,不易被检测
  • 启动参数:禁用沙箱解决环境兼容性问题,移除自动化标识、设置真实用户代理,降低反爬触发概率
  • 错误捕获:每个步骤添加try/catch,避免单个环节失败导致整个程序崩溃

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 18:11:10