User Agents

Web browsers and everyday web tech, explained

Keep Puppeteer Actions From Focusing the Browser Window

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

When you run Puppeteer scripts, every action like page.click() or page.type() can force the browser window to come to the foreground, stealing focus from whatever you are doing. This guide shows you how to run Puppeteer in a way that keeps the browser window in the background, so you can work on other tasks without interruptions.

What You'll Need

  • A Node.js project with Puppeteer installed (npm install puppeteer)
  • A basic understanding of Puppeteer's launch() and newPage() methods
  • Optional: puppeteer-extra and puppeteer-extra-plugin-stealth if you need to avoid detection

Step-by-Step

  1. Launch the browser with the headless option set to true. Headless mode runs Chromium without a visible window, so it never grabs focus. Use puppeteer.launch({ headless: true }). This is the simplest and most reliable way to avoid focus stealing, but it means you cannot see what the browser is doing.
  2. If you need a visible window but want it to stay in the background, launch with headless: false and use args: ['--window-position=-10000,-10000']. This moves the window off-screen, so it does not appear in your taskbar or steal focus. The window is technically visible but positioned far outside your screen's view.
  3. Alternatively, minimize the window immediately after launch. In your script, after const browser = await puppeteer.launch({ headless: false }), call await browser.newPage() and then use page.bringToFront()? No, that would focus it. Instead, use a system-level command to minimize the window. On Windows, you can use child_process.exec('powershell -command "(New-Object -ComObject WScript.Shell).AppActivate(\'Chrome\')"') followed by SendKeys to minimize. On macOS, use AppleScript: osascript -e 'tell application "Google Chrome" to set miniaturized of window 1 to true'. This is platform-specific and can be flaky.
  4. Use the puppeteer-extra-plugin-stealth with the useStealth option and set headless: true. This plugin helps mask headless mode, making it harder for websites to detect that you are using Puppeteer, while still keeping the browser invisible. Install with npm install puppeteer-extra puppeteer-extra-plugin-stealth and use const puppeteer = require('puppeteer-extra'); const StealthPlugin = require('puppeteer-extra-plugin-stealth'); puppeteer.use(StealthPlugin()); then launch as usual.
  5. If you must use a headed browser and need it on screen but not focused, consider running it in a separate virtual desktop or workspace. On Windows, you can use virtual-desktop tools; on macOS, use multiple Spaces. Launch the browser, then switch to another Space. This requires manual setup but works reliably.
  6. For automated tasks, consider using --disable-background-timer-throttling and --disable-renderer-backgrounding flags. These prevent the browser from slowing down when it is in the background, which is important if you need the script to run efficiently while you work elsewhere.

Common Mistakes

  • Forgetting that headless: true still uses a real Chromium instance. Some users think headless mode is a different browser, but it is just Chromium without a UI. It still executes all actions normally.
  • Using page.bringToFront() accidentally. This method explicitly focuses the page and brings the window to the foreground. Avoid calling it unless you need the browser on top.
  • Not waiting for the browser to fully launch before trying to move or minimize it. If you minimize too early, the window might not exist yet. Always await browser.newPage() first, then manipulate the window.
  • Assuming --window-position works on all operating systems. It is a Chromium flag and works on Windows and Linux, but on macOS it may not behave as expected. Test on your target OS.

Tips

  • Use headless: 'new' in newer Puppeteer versions. The new headless mode is faster and more stable than the old one. It is the default in Puppeteer v22 and later, but you can specify it explicitly.
  • Combine headless: true with --no-sandbox if you are running as root or in a container. This avoids permission issues.
  • If you need screenshots, headless mode still works perfectly. You can capture screenshots or PDFs without ever showing a window.
  • For debugging, run in headed mode temporarily. When you need to see what the browser is doing, switch to headless: false and use slowMo to watch the actions. Just remember to switch back.
  • Consider using puppeteer-cluster for parallel tasks. It manages multiple browser instances and can help distribute load without focus issues.

Troubleshooting

  • If the window still steals focus despite using --window-position, try adding --start-minimized to the args. This flag starts the window minimized, which usually prevents focus stealing.
  • If Puppeteer fails to launch with headless: true on some sites, it might be due to bot detection. Use the stealth plugin or try the new headless mode. Also, check that your user-agent is not the default Puppeteer one.
  • If the browser window appears but does not stay minimized, your script might be calling page.bringToFront() indirectly. Some actions like page.waitForNavigation() do not focus, but page.click() on a link that opens a new tab might. Check your code for target.createPage() or page.bringToFront().
  • If you are on Linux and the window still pops up, try using a virtual framebuffer like xvfb. Run your script with xvfb-run -a node script.js to run the browser in a virtual display, which never appears on your screen.
  • If the script runs but the browser is slow in the background, add the flags mentioned in step 6. Also ensure your system is not aggressively throttling background processes.

Frequently Asked Questions

Does headless mode really prevent focus stealing?

Yes, because there is no window to focus. All actions happen in a hidden Chromium process.

Can I still take screenshots in headless mode?

Absolutely. Screenshots, PDFs, and all DOM interactions work exactly the same in headless mode.

Will websites detect that I'm using headless Chrome?

Some can, but using the stealth plugin and setting a realistic user-agent reduces the chance. It is not foolproof, but it works for most sites.

Is there a way to run Puppeteer without any window at all?

Yes, that is exactly what headless mode does. It runs the browser without any GUI.

What if I need to see the browser for debugging but not have it steal focus?

Use the off-screen positioning trick (--window-position=-10000,-10000) or run it on a separate virtual desktop. You can also take screenshots at intervals to see what's happening.

Does Puppeteer have a built-in option to disable focus?

No, there is no direct option. You have to use the workarounds described above, like headless mode or window positioning.