Puppeteer C#:连接到正在运行的Chrome实例 [英] Puppeteer C#: Connecting to Running Chrome Instance

查看:586
本文介绍了Puppeteer C#:连接到正在运行的Chrome实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在运行PuppeteerSharp v1.19.0来启动浏览器并抓取网页.迫切需要能够连接到现有的chrome实例并自动执行任务.如何实现这一PuppeteerSharp?通过以下操作,我可以使用PuppeteersSharp启动Chrome而不是Chromium,但是我没有找到如何连接到现有的Chrome实例的方法.感谢所有帮助.

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();

推荐答案

首先,您需要在启用了远程调试的情况下启动Chrome.例如,如果使用标志-remote-debugging-port = 2122 启动Chrome,则可以使用 http://127.0连接到浏览器.0.1:2122 URL.

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.

第二,如果要连接到现有浏览器,则需要调用 ConnectAsync ,而不是 LaunchAsync .

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"
}));

这篇关于Puppeteer C#:连接到正在运行的Chrome实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆