Rayrun
← Back to Discord Forum

Connecting to an existing browser session

Just wanted to double check that Playwright cannot connect to an existing browser session at all.

This thread is trying to answer question "Can Playwright connect to an existing browser session?"

2 replies
aad_aka_2733
aad_aka_2733
@Rakesh Nbr: That is an incorrect assumption. Playwrigth CAN connect to an existing browser but you have to start that browser in a special way. For example for Brave and Chrome I use the command line argument --remote-debugging-port=9222 and in the playwright code the following sequence to get playwright loaded and connect to that existing browser: var pw = await Playwright.CreateAsync(); _browser = await pw.Chromium.ConnectOverCDPAsync("http://localhost:9222"); Console.WriteLine($"browser.Version={_browser.Version}"); Console.WriteLine($"browser.Contexts.Count={_browser.Contexts.Count}"); // var context = _browser.Contexts[0]; Console.WriteLine($"context.Pages.Count={context.Pages.Count}"); // IPage this_page = context.Pages[0];

That is an incorrect assumption. Playwrigth CAN connect to an existing browser but you have to start that browser in a special way. For example for Brave and Chrome I use the command line argument --remote-debugging-port=9222 and in the playwright code the following sequence to get playwright loaded and connect to that existing browser:

var pw = await Playwright.CreateAsync(); _browser = await pw.Chromium.ConnectOverCDPAsync("http://localhost:9222"); Console.WriteLine($"browser.Version={_browser.Version}"); Console.WriteLine($"browser.Contexts.Count={_browser.Contexts.Count}"); // var context = _browser.Contexts[0]; Console.WriteLine($"context.Pages.Count={context.Pages.Count}"); // IPage this_page = context.Pages[0];

Okay i'm lazy 🙂 Thanks for the pointer @aad_aka_2733. If it helps anyone else. Found i would fail if i had any other instance of chrome running. With my luck i will always get the instance without the remote debuggering enabled, ah no reason one can't go for a free port to run Chrome debugger on as needed and avoid the issue altogether...

For others i'm assuming the default location for Chrome, may need to change for your use, but got this working just fine.

Thanks so much...

public async Task<IPlaywright> LaunchChromeWithDebuggerAsync(int port =9222) { var psi = new ProcessStartInfo(); psi.UseShellExecute = false; psi.FileName = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"; psi.Arguments = $"--remote-debugging-port={port}"; Process p = new Process(); p.StartInfo = psi; p.Start(); IPlaywright pw = await Playwright.CreateAsync(); IBrowser browser = await pw.Chromium.ConnectOverCDPAsync($"http://localhost:{port}"); return pw; }

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.