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

如何在Mac OS后台启动非无头模式的Puppeteer?

解决Puppeteer非无头模式窗口前置问题

当Puppeteer设置headless: false启动Chrome时,窗口会自动前置抢占焦点,以下是几种满足需求的实现方案:

1. 让Chrome窗口后台运行(位于其他窗口之后)

通过Chrome命令行参数配置,让窗口启动时不获取焦点并处于后台:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
    headless: false,
    args: [
      '--start-in-background',
      '--no-focus-on-launch'
    ]
  });
  const page = await browser.newPage();
  // 执行你的页面操作
})();

如果--start-in-background在部分版本无效,可补充--window-position=-32000,-32000将窗口移至屏幕外,需要时通过await page.bringToFront()调回可见区域。

2. 启动时直接最小化Chrome

使用--start-minimized参数,让Chrome启动后直接最小化到任务栏(Mac为Dock):

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
    headless: false,
    args: ['--start-minimized']
  });
  const page = await browser.newPage();
  // 执行你的页面操作
})();

3. 在Mac的非活跃桌面启动Chrome

借助AppleScript配合Puppeteer,将Chrome窗口移动到非活跃桌面:

const puppeteer = require('puppeteer');
const { execSync } = require('child_process');

(async () => {
  // 启动Chrome并开启远程调试
  const browser = await puppeteer.launch({
    headless: false,
    args: ['--remote-debugging-port=9222']
  });

  // 通过AppleScript将Chrome窗口移到第二个桌面(可根据实际调整桌面序号)
  execSync(`
    tell application "System Events"
      set chromeWindow to first window of process "Google Chrome"
      tell application "Mission Control" to activate
      delay 0.5
      drag chromeWindow to the second desktop
      delay 0.5
    end tell
  `);

  const page = await browser.newPage();
  // 执行你的页面操作
})();

也可以先创建新桌面再启动Chrome,适合需要独立隔离环境的场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 22:28:22