How do I set a custom user agent in Puppeteer?
Antek N
antek n profile pic

To set a custom user agent in Puppeteer, you can utilize thepage.setUserAgent() method. Here's a detailed explanation of how to set a custom user agent: 1. Launching a new browser instance and creating a new page:

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

   const puppeteer = require('puppeteer');

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

     // Perform actions with the page here

     // Close the browser
     await browser.close();
   })();
   

This code sets up a basic Puppeteer script. It launches a new headless browser instance and creates a new page to work with. 2. Setting a custom user agent:

1
2
3
4
5

   const userAgent = 'Your Custom User Agent';

   await page.setUserAgent(userAgent);
   

ThesetUserAgent() method is used to set a custom user agent for the page. Provide your desired user agent string as a parameter to the method. For example, you can set the user agent to mimic a specific browser and version:

1
2
3

   await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36');
   

Or you can set a custom user agent to provide any other value you desire. 3. Verifying the user agent: To confirm that the user agent has been set correctly, you can navigate to a page that displays user agent information and retrieve it usingpage.evaluate():

1
2
3
4
5
6

   await page.goto('https://example.com');

   const userAgent = await page.evaluate(() => window.navigator.userAgent);
   console.log('Current User Agent:', userAgent);
   

In this example, the page navigates tohttps://example.com, and thenpage.evaluate() is used to retrieve thewindow.navigator.userAgent property, which represents the user agent. The user agent is then logged to the console for verification. By following these steps, you can set a custom user agent in Puppeteer using thesetUserAgent() method. This allows you to emulate different browsers, versions, or any desired user agent to suit your testing or scraping needs.