在 Puppeteer Sharp 中程序完成时如何阻止浏览器关闭
How to stop browser from closing when the program completes in Puppeteer Sharp
我们正在为我们的客户提供密码管理解决方案。我们能够使用以下代码使用 puppeter sharp 成功地自动化用户名和密码。
但是程序执行后浏览器自动关闭。程序执行后有没有办法退出浏览器运行
static async Task Main(string[] args)
{
var options = new LaunchOptions { Headless = false, ExecutablePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", Args = new string[] { "--no-sandbox" } };
using (var browser = await Puppeteer.LaunchAsync(options))
using (var page = await browser.NewPageAsync())
{
// use page
await page.GoToAsync("https://accounts.google.com/");
//await page.WaitForNavigationAsync();
// await page.WaitForSelectorAsync("type=email");
// await page.ClickAsync("type=email");
// await page.WaitForNavigationAsync();
await Task.Delay(2000);
//TODO : change to your email
await page.TypeAsync("#identifierId", "someusername@gmail.com");
await page.WaitForSelectorAsync("#identifierNext");
await page.ClickAsync("#identifierNext");
await Task.Delay(2000);
await page.WaitForSelectorAsync(@"input[type='password']");
await Task.Delay(2000);
await page.ClickAsync(@"input[type='password']");
await Task.Delay(2000);
//TODO : change to your password
await page.TypeAsync(@"input[name='password']", "somepassword");
await Task.Delay(2000);
await page.WaitForSelectorAsync("#passwordNext");
await Task.Delay(2000);
await page.ClickAsync("#passwordNext");
await Task.Delay(2000);
await page.WaitForNavigationAsync();
}
}
如果您想自动化浏览器,然后让它打开以供用户交互,您需要在 自动化之前启动浏览器 ,并将您的代码附加到该浏览器实例。这个问题提供了一些关于如何做到这一点的信息:
Puppeteer C#: Connecting to Running Chrome Instance
本文更详细地介绍了该策略:
https://medium.com/@jaredpotter1/connecting-puppeteer-to-existing-chrome-window-8a10828149e0
我们正在为我们的客户提供密码管理解决方案。我们能够使用以下代码使用 puppeter sharp 成功地自动化用户名和密码。
但是程序执行后浏览器自动关闭。程序执行后有没有办法退出浏览器运行
static async Task Main(string[] args)
{
var options = new LaunchOptions { Headless = false, ExecutablePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", Args = new string[] { "--no-sandbox" } };
using (var browser = await Puppeteer.LaunchAsync(options))
using (var page = await browser.NewPageAsync())
{
// use page
await page.GoToAsync("https://accounts.google.com/");
//await page.WaitForNavigationAsync();
// await page.WaitForSelectorAsync("type=email");
// await page.ClickAsync("type=email");
// await page.WaitForNavigationAsync();
await Task.Delay(2000);
//TODO : change to your email
await page.TypeAsync("#identifierId", "someusername@gmail.com");
await page.WaitForSelectorAsync("#identifierNext");
await page.ClickAsync("#identifierNext");
await Task.Delay(2000);
await page.WaitForSelectorAsync(@"input[type='password']");
await Task.Delay(2000);
await page.ClickAsync(@"input[type='password']");
await Task.Delay(2000);
//TODO : change to your password
await page.TypeAsync(@"input[name='password']", "somepassword");
await Task.Delay(2000);
await page.WaitForSelectorAsync("#passwordNext");
await Task.Delay(2000);
await page.ClickAsync("#passwordNext");
await Task.Delay(2000);
await page.WaitForNavigationAsync();
}
}
如果您想自动化浏览器,然后让它打开以供用户交互,您需要在 自动化之前启动浏览器 ,并将您的代码附加到该浏览器实例。这个问题提供了一些关于如何做到这一点的信息:
Puppeteer C#: Connecting to Running Chrome Instance
本文更详细地介绍了该策略:
https://medium.com/@jaredpotter1/connecting-puppeteer-to-existing-chrome-window-8a10828149e0