How do I handle page timeouts in Puppeteer?
Ava W
ava w profile pic

To set cookies in Puppeteer, you can use thepage.setCookie method provided by the Puppeteer API. This method allows you to add individual cookies to a specific page. Here's a step-by-step guide on how to set cookies in Puppeteer: Step 1: Launch Puppeteer and navigate to a page First, you need to launch Puppeteer and navigate to the desired page. Here's an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  // Navigate to a specific page
  await page.goto('https://www.example.com');

  // Continue with setting cookies

  await browser.close();
})();

Step 2: Set the cookies To set cookies, you need to use thepage.setCookie method. This method accepts an array of cookie objects that define the cookie properties. Here's an example of setting two cookies:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

const cookies = [
  {
    name: 'cookie1',
    value: 'value1',
    domain: 'www.example.com',
    path: '/',
    expires: Math.floor(Date.now() / 1000) + 3600, // expires in 1 hour
    secure: true,
    httpOnly: false
  },
  {
    name: 'cookie2',
    value: 'value2',
    domain: 'www.example.com',
    path: '/',
    expires: Math.floor(Date.now() / 1000) + 7200, // expires in 2 hours
    secure: false,
    httpOnly: true
  }
];

for (const cookie of cookies) {
  await page.setCookie(cookie);
}

In the above example, we define two cookies using objects. Each cookie object should have the following properties: -name (required): The name of the cookie. -value (required): The value of the cookie. -domain (optional): The domain for which the cookie is valid. If not specified, it defaults to the current domain. -path (optional): The path within the domain for which the cookie is valid. If not specified, it defaults to'/'. -expires (optional): The expiration time of the cookie in Unix time (seconds since January 1, 1970). If not specified, the cookie will be a session cookie. -secure (optional): Boolean value indicating if the cookie should only be transmitted over HTTPS. Default isfalse. -httpOnly (optional): Boolean value indicating if the cookie should be accessible only through HTTP requests. Default isfalse. You can add as many cookies as you need by appending more objects to thecookies array. Step 3: Use the cookies After setting the cookies, you can utilize them in subsequent requests made by the page. The cookies will be automatically sent along with the requests. Here's an example of printing the cookies after setting them:

1
2
3

const pageCookies = await page.cookies();
console.log(pageCookies);

This will print an array of cookie objects currently set in the page. By following these steps, you can easily set cookies in Puppeteer using thepage.setCookie method. Remember to navigate to the desired page before setting the cookies and make sure to set the cookies before making any requests that require them.