Search code examples
c#puppeteerpuppeteer-sharp

Is there a remove page method corresponding to NewPageAsync() in PuppeteerSharp?


I keep the browser object as a singleton and would like to call NewPageAsync() for potentially thousands of urls. Is there a way to cleanup the page after it is used?


Solution

  • You can close the page using CloseAsync:

    var page = browser.NewPageAsync();
    ////
    await page.CloseAsync();
    

    An using block will also close the page:

    using (var page = await new browser.PageAsync())
    {
    ///
    }
    

    Puppeteer-Sharp v2.0.3+ also supports await using blocks

    await using (var page = await new browser.PageAsync())
    {
     ///
    }