In Puppeteer-Sharp, which is a .NET port of the Node.js library Puppeteer, managing cookies involves using the Page
class's cookie-related methods. Here's how you can manage cookies in Puppeteer-Sharp:
Setting Cookies
To set cookies, you can use the SetCookieAsync
method. You'll need to create CookieParam
objects that represent the cookies you want to set.
using PuppeteerSharp;
// ... other code ...
var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });
var page = await browser.NewPageAsync();
var cookieParam = new CookieParam
{
Name = "myCookie",
Value = "myValue",
Domain = "example.com"
};
await page.SetCookieAsync(cookieParam);
// ... other code ...
Getting Cookies
To get cookies, you can use the GetCookiesAsync
method. If you want to get cookies for a specific URL, you can pass the URL as a parameter.
// ... other code ...
var cookies = await page.GetCookiesAsync(new[] { "https://example.com" });
foreach (var cookie in cookies)
{
Console.WriteLine($"Name: {cookie.Name}, Value: {cookie.Value}");
}
// ... other code ...
Deleting Cookies
To delete cookies, you can use the DeleteCookieAsync
method. Similar to setting cookies, you'll need to create CookieParam
objects for the cookies you want to delete.
// ... other code ...
var cookieToDelete = new CookieParam
{
Name = "myCookie",
Domain = "example.com"
};
await page.DeleteCookieAsync(cookieToDelete);
// ... other code ...
Clearing All Cookies
If you want to clear all cookies for the session, you can use the BrowserContext
class's ClearCookiesAsync
method.
// ... other code ...
await page.BrowserContext.ClearCookiesAsync();
// ... other code ...
Example: Managing Session Cookies
Here's an example that demonstrates setting, getting, and deleting a cookie in a session:
using System;
using System.Threading.Tasks;
using PuppeteerSharp;
class Program
{
static async Task Main(string[] args)
{
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });
var page = await browser.NewPageAsync();
// Set a cookie
var setCookie = new CookieParam
{
Name = "sessionCookie",
Value = "123456",
Domain = "example.com"
};
await page.SetCookieAsync(setCookie);
// Get and display the cookie
var getCookies = await page.GetCookiesAsync(new[] { "http://example.com" });
foreach (var cookie in getCookies)
{
Console.WriteLine($"Set Cookie: {cookie.Name} = {cookie.Value}");
}
// Delete the cookie
var deleteCookie = new CookieParam
{
Name = "sessionCookie",
Domain = "example.com"
};
await page.DeleteCookieAsync(deleteCookie);
// Try to get the cookie after deletion
getCookies = await page.GetCookiesAsync(new[] { "http://example.com" });
if (getCookies.Length == 0)
{
Console.WriteLine("Cookie successfully deleted.");
}
else
{
Console.WriteLine("Cookie is still present!");
}
await browser.CloseAsync();
}
}
In this example, the BrowserFetcher
is used to download the required version of Chromium if it's not already present. Then, a cookie named sessionCookie
is set, retrieved, and then deleted. Finally, we attempt to retrieve the cookie again to confirm it has been deleted.
Please note that Puppeteer-Sharp is an asynchronous library, so you must use async
methods and await
them properly. Also, ensure that you have the necessary using PuppeteerSharp;
at the top of your code to include the Puppeteer-Sharp namespace.