我们正在为我们的客户提供密码管理解决方案。我们能够使用以下代码使用 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();
}
}