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

1.7k views Asked by At

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?

1

There are 1 answers

7
Stefan On BEST ANSWER

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.