Keep Puppeteer Actions From Focusing the Browser Window
In this article
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()andnewPage()methods - Optional:
puppeteer-extraandpuppeteer-extra-plugin-stealthif you need to avoid detection
Step-by-Step
- Launch the browser with the
headlessoption set totrue. Headless mode runs Chromium without a visible window, so it never grabs focus. Usepuppeteer.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. - If you need a visible window but want it to stay in the background, launch with
headless: falseand useargs: ['--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. - Alternatively, minimize the window immediately after launch. In your script, after
const browser = await puppeteer.launch({ headless: false }), callawait browser.newPage()and then usepage.bringToFront()? No, that would focus it. Instead, use a system-level command to minimize the window. On Windows, you can usechild_process.exec('powershell -command "(New-Object -ComObject WScript.Shell).AppActivate(\'Chrome\')"')followed bySendKeysto 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. - Use the
puppeteer-extra-plugin-stealthwith theuseStealthoption and setheadless: 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 withnpm install puppeteer-extra puppeteer-extra-plugin-stealthand useconst puppeteer = require('puppeteer-extra'); const StealthPlugin = require('puppeteer-extra-plugin-stealth'); puppeteer.use(StealthPlugin());then launch as usual. - 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-desktoptools; on macOS, use multiple Spaces. Launch the browser, then switch to another Space. This requires manual setup but works reliably. - For automated tasks, consider using
--disable-background-timer-throttlingand--disable-renderer-backgroundingflags. 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: truestill 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-positionworks 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: truewith--no-sandboxif 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: falseand useslowMoto watch the actions. Just remember to switch back. - Consider using
puppeteer-clusterfor 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-minimizedto the args. This flag starts the window minimized, which usually prevents focus stealing. - If Puppeteer fails to launch with
headless: trueon 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 likepage.waitForNavigation()do not focus, butpage.click()on a link that opens a new tab might. Check your code fortarget.createPage()orpage.bringToFront(). - If you are on Linux and the window still pops up, try using a virtual framebuffer like
xvfb. Run your script withxvfb-run -a node script.jsto 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.