Disconnect from a remote Chrome browser
Problem
Section titled “Problem”Your app attaches to Chrome that another process or service started. The app needs to release its connection when its work ends so a second client can attach to the same running browser.
With Puppeteer Sharp 25.19.0, synchronous using on an attached IBrowser invokes browser close. Call Disconnect() explicitly when the browser belongs to another process. Close any scratch tabs your app owns before disconnecting.
Connect and verify the handoff
Section titled “Connect and verify the handoff”Use a disposable Chrome profile for this example. Set PUPPETEER_CDP_URL to the browser-level WebSocket URL. For a locally started Chrome with a debugging port, read webSocketDebuggerUrl from /json/version on that port. For a hosted browser, use the complete endpoint issued by your provider. Some wss:// URLs contain access credentials. Keep the value out of source code and logs.
Tested with Puppeteer Sharp 25.19.0 and .NET 10 against local Chrome and an authenticated WSS endpoint. The Cloudbrowser CDP connection guide shows how to obtain the hosted endpoint used in the second test.
using PuppeteerSharp;
var endpoint = Environment.GetEnvironmentVariable("PUPPETEER_CDP_URL") ?? throw new InvalidOperationException("Set PUPPETEER_CDP_URL first.");var options = new ConnectOptions { BrowserWSEndpoint = endpoint };var marker = $"Handoff {Guid.NewGuid()}";
var first = await Puppeteer.ConnectAsync(options);try{ var page = await first.NewPageAsync(); await page.EvaluateFunctionAsync("marker => { document.title = marker; }", marker);}finally{ first.Disconnect();}
var second = await Puppeteer.ConnectAsync(options);try{ foreach (var page in await second.PagesAsync()) { if (await page.GetTitleAsync() != marker) { continue; }
await page.CloseAsync(); Console.WriteLine("Reattached to the same tab."); return; }
throw new InvalidOperationException("The handoff tab was not found.");}finally{ second.Disconnect();}The first client leaves a tab with a unique title. The second client finds it, closes that tab, and disconnects. The browser owner controls when Chrome stops.
On Puppeteer Sharp 25.19.0, await using also detached when tested with a CDP browser obtained through ConnectAsync. The explicit call makes the intended cleanup clear at the call site.
Made with love by Dario Kondratiuk