首页 > 解决方案 > Puppeteer sharp is opening 2 chrome windows on incognito

问题描述

I've just started using this port for puppeteer but I have a problem. When I put the flag "--incognito" or if I use the browser.CreateIncognitoBrowserContextAsync() I always get 2 chrome windows opened. There is a fix for this issue ? If I do this to my chrome broswer using "--incognito" flag it will open only 1 instance.

标签: c#puppeteerincognito-mode

解决方案


它相当混乱,但这似乎有效..

using (Browser browser = await Puppeteer.LaunchAsync(options))
{
     // create the async context 
    var context = await browser.CreateIncognitoBrowserContextAsync();

    // get the page created by default when launch async ran and close it whilst keeping the browser active
    var browserPages = await browser.PagesAsync();
    await browserPages[0].CloseAsync();

    // create a new page using the incognito context
    using (Page page = await context.NewPageAsync())
    {
        // do something 
    }
}

推荐阅读