I’m wondering if there’s a way to attach Puppeteer to a browser that’s already running instead of creating a new one through code. Let’s say I have Chrome open and I’m browsing normally as a user. Can I somehow hook into that existing browser session with my Puppeteer script? I’ve been launching browsers through puppeteer.launch() but I want to work with a browser that’s already started. Is this something that can be done, and if so, what’s the proper approach? I need to automate some tasks on an existing browser session rather than starting fresh each time.
There’s also getBrowserWSEndpoint() for reconnecting later. I store the websocket URL when I first connect so I can reattach if my script crashes. Just keep Chrome open - otherwise you’ll hit connection refused errors.
Yeah, you can connect to an existing Chrome instance, but Chrome needs remote debugging enabled first. Launch Chrome from command line with --remote-debugging-port=9222, then use puppeteer.connect() with the debugging URL instead of puppeteer.launch(). I’ve done this when I wanted to keep login sessions going and pick up where I left off manually. Here’s the catch - you can’t just connect to any Chrome window that’s already open. It has to be started with debugging enabled from the start. Once you’re connected, Puppeteer works the same as launching through code. Just handle connection errors gracefully since the browser might close unexpectedly.
Here’s another approach - use Chrome’s existing user data directory. When you start Chrome with debugging enabled, add --user-data-dir pointing to your regular Chrome profile. You’ll get all your cookies, saved passwords, and browsing data. Really helpful when automating tasks that need you logged into multiple services. The command looks like chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\Users\YourName\AppData\Local\Google\Chrome\User Data". Close all other Chrome instances first or you’ll hit a profile lock error. Connection with puppeteer.connect() stays the same, but now your script runs in your actual browsing environment instead of starting fresh.