问题
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();
回答1:
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"
}));
来源:https://stackoverflow.com/questions/57876123/puppeteer-c-connecting-to-running-chrome-instance