Search code examples
c#web-scrapingpuppeteer-sharpdatascript

How can I get cookies from Puppeteer chrome session C#?


I need to get cookies of particular website from puppeteer chrome session and add these cookies to script.Here is code I am doing to get cookies form page:

page.GetCookiesAsync();

But it return:

Id = 7315, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"

Other way I have tried:

page.Client.SendAsync("Network.getAllCookies");

Both methods not working for me. What I am doing wrong?


Solution

  • The execution of the GetCookiesAsync task needs to be awaited, like this:

    private async Task YourMethod()
    { 
        var result = await page.GetCookiesAsync();
    }
    

    You might need to change your callers for this.

    Try to read about asynchronous programming in C#: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/


    So what you are seeing with this:

    Id = 7315, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"

    is the async task not yet completed. GetCookiesAsync returns immediately. If you want to want to wait for the result, you should await it.