2

I am currently running PuppeteerSharp v1.19.0 to launch a browser and scrape web pages. The need has come up to be able to connect to an existing chrome instance and automate tasks. How can I achieve this one PuppeteerSharp? Via the following, I'm able to launch Chrome instead of Chromium with PuppeteersSharp but I haven't found how I'm able to connect to an existing instance of Chrome. All help is appreciated.

using PuppeteerSharp;

new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision).GetAwaiter().GetResult();
_browser = Puppeteer.LaunchAsync(new LaunchOptions { Headless = false, ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" }).GetAwaiter().GetResult();
GregH
  • 5,125
  • 8
  • 55
  • 109

1 Answers1

4

First thing, you need to launch Chrome with remote debugging enabled. If you launch Chrome, for instance, with the flag --remote-debugging-port=2122, you know that you will be able to connect to the browser using the http://127.0.0.1:2122 URL.

Second, if you want to connect to an existing browser you need to call ConnectAsync instead of LaunchAsync.

Having all that. If would be a matter of doing something like this:

var browser = await Puppeteer.ConnectAsync(new ConnectOptions
{
    BrowserURL = "http://127.0.0.1:2122"
}));
Alexandre
  • 7,004
  • 5
  • 54
  • 72
hardkoded
  • 18,915
  • 3
  • 52
  • 64
  • 1
    if I launch chrome on windows via `start chrome.exe --remote-debugging-port=9222` and visit `http://localhost:9222`, I get a `this site cant be reached` error – GregH Sep 10 '19 at 22:37
  • @GregH Just go to `http://127.0.0.1:9222/json/version`. Read [this article](https://medium.com/@jaredpotter1/connecting-puppeteer-to-existing-chrome-window-8a10828149e0) – ABS Dec 06 '20 at 10:47
  • i have the same problem. Could not connect to chrome – Bretbas Dec 25 '22 at 11:12