Puppeteer C#: Connecting to Running Chrome Instance

别等时光非礼了梦想. 提交于 2021-01-24 07:09:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!