1

We are doing password management solutions for our client. We are able to sucessfully automate the username and password using puppeter sharp using the following code.

But after the program execution the browser closes automatically. Is there anyway to leave the browser running after program execution.

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();
            }
        }
  • Does this answer your question? [Puppeteer C#: Connecting to Running Chrome Instance](https://stackoverflow.com/questions/57876123/puppeteer-c-connecting-to-running-chrome-instance) – hardkoded Sep 14 '20 at 15:42

1 Answers1

0

If you want to automate a browser, then leave it open for user interaction, you will need to launch the browser before automating it, and have your code attach to that browser instance. This question gives some info on how to do that:

Puppeteer C#: Connecting to Running Chrome Instance

And this article goes into greater detail on that strategy:

https://medium.com/@jaredpotter1/connecting-puppeteer-to-existing-chrome-window-8a10828149e0

Todd Price
  • 2,650
  • 1
  • 18
  • 26
  • Hi Todd, Thank you very much for your help. I have tried the above method to launch the browser manually from the command line and executed the following code to the browser. But after executing the code the browser is getting closed after program execution. – karthick raja Sep 15 '20 at 05:53
  • It's probably the `using` statement for the browser variable that is causing the browser to close. That would forces .NET to call the IDisposable method, which is probably what is closing your browser. – Todd Price Sep 15 '20 at 14:42
  • Thanks Todd. It resolved the issue. The problem is using statement. – karthick raja Sep 22 '20 at 08:03