Run Puppeteer Inside Chrome for Hybrid Automations
In this article
Running Puppeteer inside Chrome—rather than as a standalone Node.js process—lets you combine the browser's native debugging protocol with your own automation logic. This hybrid approach is useful when you need to interact with a page that is already open, control multiple tabs, or reuse an existing Chrome profile with logged-in sessions. You can achieve this by launching Chrome with remote debugging enabled and connecting Puppeteer to it via puppeteer.connect(). Below is a step-by-step guide to set this up.
What You'll Need
- Node.js installed (version 12 or later).
- Puppeteer installed in your project:
npm install puppeteer(orpuppeteer-coreif you already have Chrome). - Google Chrome or Chromium installed on your system.
- Basic familiarity with JavaScript and the command line.
Step-by-Step
- Locate your Chrome executable. On Windows it's usually
C:\Program Files\Google\Chrome\Application\chrome.exe; on macOS it's/Applications/Google Chrome.app/Contents/MacOS/Google Chrome; on Linux it's often/usr/bin/google-chromeor/usr/bin/chromium. - Launch Chrome with remote debugging enabled. Open a terminal and run:
chrome --remote-debugging-port=9222 --user-data-dir=/tmp/chrome-profile
Replacechromewith the full path to your executable. The--user-data-dirflag uses a separate profile so your normal browsing session isn't affected. Keep this terminal running. - Verify debugging is active. In another terminal, run
curl http://localhost:9222/json/version. You should see a JSON response with awebSocketDebuggerUrl. - Create your Node.js script. In your project folder, create a file, e.g.,
hybrid.js, and require Puppeteer: - Connect to the running Chrome instance. Use
puppeteer.connect()with the browser URL. For example:const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.connect({ browserURL: 'http://localhost:9222' }); const pages = await browser.pages(); console.log('Connected. Open pages:', pages.length); // Your automation code here await browser.disconnect(); })(); - Interact with existing tabs. Use
browser.pages()to get an array of open tabs. You can select one by URL or title and then use Puppeteer's page methods (e.g.,page.goto(),page.click()) on it. - Create new tabs if needed. Use
browser.newPage()to open a new tab in the same Chrome window. This is useful for hybrid workflows where you want to keep your manual session intact. - Run your script. Execute
node hybrid.jsfrom the terminal. You'll see the automation act on the already-open Chrome window.
Common Mistakes
- Using the default Chrome profile. If you don't specify
--user-data-dir, Chrome may refuse to start with debugging enabled because it detects an existing running instance. Always use a separate profile. - Forgetting to keep the Chrome terminal open. The debugging port is only available while that Chrome process runs. If you close it, the connection fails.
- Mixing Puppeteer and puppeteer-core. If you use
puppeteer(which downloads its own Chromium), it may try to launch its own browser instead of connecting. Usepuppeteer-coreif you don't want the bundled Chromium, or ensure you callconnect()and notlaunch(). - Assuming the browser is already connected. You must explicitly connect in your script; Puppeteer doesn't auto-detect the running Chrome.
Tips
- Use a dedicated debugging port like
9222to avoid conflicts with other tools. - Close the debugging Chrome session properly with
browser.disconnect()to free resources. - If you need to automate a page that requires login, log in manually in the debugging Chrome window first, then run your script—the session cookies are preserved.
- For headless hybrid automation, you can add
--headlesswhen launching Chrome, but then you won't see the window. Only use it if you don't need visual feedback. - Check the list of targets via
http://localhost:9222/jsonto see all open tabs and their IDs; you can use that to target specific pages.
Troubleshooting
- Connection refused error. Make sure Chrome is running with the debugging port. Run
curl http://localhost:9222/json/versionto test. If it fails, restart Chrome with the correct flags. - Chrome doesn't start with debugging. Close all Chrome instances, then launch with
--user-data-dirpointing to a new folder. If the issue persists, check that the path to the executable is correct. - Puppeteer launches a new browser instead of connecting. Double-check you are using
puppeteer.connect(), notpuppeteer.launch(). Also, ensure you've passed the correctbrowserURL. - Pages array is empty. If Chrome is running but has no open tabs,
browser.pages()may return an empty array. Open a tab manually or usebrowser.newPage()to create one. - Automation works but doesn't see the page you want. Use
page.url()to check which page you're on. You may need to filter pages by URL or title.
Frequently Asked Questions
Can I use this method to automate a page that is already open in my normal Chrome?
Yes, but you need to launch Chrome with the debugging flag and a separate user data directory. You cannot attach to a regular Chrome instance that wasn't started with debugging enabled.
What is the difference between Puppeteer and Puppeteer-core here?
Puppeteer downloads its own Chromium by default. Puppeteer-core does not. For connecting to an existing Chrome, either works, but puppeteer-core is lighter if you already have Chrome installed.
Can I run multiple Puppeteer scripts against the same Chrome instance?
Yes, multiple scripts can connect to the same debugging port simultaneously. However, be careful about concurrent actions on the same page—they may conflict.
Does this work in headless mode?
Yes, you can launch Chrome with --headless along with the debugging flags. The automation will work the same, but you won't see the browser window.
How do I stop the debugging session?
Simply close the Chrome window or terminate the process. In your script, call browser.disconnect() to end the Puppeteer connection without closing the browser.
Can I use this to automate a mobile view?
Yes, you can use Puppeteer's device emulation features, like page.setViewport(), even on a connected browser. Just be aware that the viewport applies to that page only.