How to Manage User-Agent in Puppeteer

SwiftProxy
By - Emily Chan
2025-03-05 14:47:46

How to Manage User-Agent in Puppeteer

Web scraping, automated testing, performance monitoring—Puppeteer is a powerhouse for developers. But one critical aspect often overlooked? The user-agent.
Every time a browser makes a request, it sends a user-agent string. This string identifies the browser, version, operating system, and sometimes even the device model. Websites use it to serve different content, optimize layouts, or, more importantly, detect and block bots.
If you're using Puppeteer and haven't thought about user-agent manipulation, you might already be on a website's radar. Let's fix that.

When to Use a Random or Custom User-Agent in Puppeteer

It depends on what you're doing.

When to Use a Random User-Agent

If you're web scraping, sticking with the same user-agent for every request is a red flag. Websites can detect repeated requests from the same user-agent and block you. The solution? Rotate user-agents. Using a different one for each request makes you harder to track.

When to Use a Custom User-Agent

If you're automating tests, you want consistency. A static user-agent ensures predictable results and easier debugging. If a web app behaves differently across browsers, you can set your user-agent to mimic different environments and test accordingly.

Setting a Random User-Agent in Puppeteer

To randomly change your user-agent for each request, use the user-agents package:

const puppeteer = require('puppeteer');  
const { UserAgent } = require('user-agents');  

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

  const userAgent = new UserAgent({ deviceCategory: 'desktop' }).toString();  
  await page.setUserAgent(userAgent);  

  await page.goto('https://example.com');  
  // Perform web scraping or automation tasks.  

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

Step-by-Step Breakdown:

Install Puppeteer & Dependencies

npm install puppeteer user-agents  

Import Required Packages

const puppeteer = require('puppeteer');  
const { UserAgent } = require('user-agents');  

Generate a Random User-Agent

const userAgent = new UserAgent({ deviceCategory: 'desktop' }).toString();  

Apply it to Your Puppeteer Page

await page.setUserAgent(userAgent);  

Navigate and Perform Actions

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

Setting a Custom User-Agent in Puppeteer

If you need control over the user-agent, manually set it:

const puppeteer = require('puppeteer');  

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

  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'  
  );  

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

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

Key Steps:

Install Puppeteer

npm install puppeteer  

Import Puppeteer

const puppeteer = require('puppeteer');  

Set a Custom User-Agent

await page.setUserAgent('Your_Custom_User_Agent');  

Avoiding Common Pitfalls

Even with proper user-agent handling, you might still hit roadblocks. Here’s how to tackle them:

Getting Blacklisted

Websites monitor behavior beyond user-agent strings. If they detect automation, they might block your IP.
Solution: Rotate IPs using proxies, implement delays, and mimic human behavior.

Incorrect User-Agent Format

Some sites reject invalid user-agent formats.
Solution: Use real browser user-agents from trusted sources.

Rate Limiting

Even with random user-agents, sending too many requests quickly can trigger restrictions.
Solution: Space out requests with setTimeout() and respect rate limits.

Custom User-Agent Not Recognized

Some websites validate user-agents against known lists.
Solution: Use legitimate user-agent strings and update them regularly.

API Changes

Libraries for generating user-agents may become outdated.
Solution: Regularly update dependencies and verify their functionality.

Final Thoughts

Choosing between a random or custom user-agent depends on your Puppeteer project's goals. If you need anonymity and dynamic requests, rotating user-agents is the best choice. For consistency, a fixed user-agent is more suitable.
Web automation requires attention to detail. Mastering user-agents helps you gain better access, avoid blocks, and improve automation performance. Optimizing your Puppeteer scripts ensures smoother operation.

About the author

SwiftProxy
Emily Chan
Lead Writer at Swiftproxy
Emily Chan is the lead writer at Swiftproxy, bringing over a decade of experience in technology, digital infrastructure, and strategic communications. Based in Hong Kong, she combines regional insight with a clear, practical voice to help businesses navigate the evolving world of proxy solutions and data-driven growth.
The content provided on the Swiftproxy Blog is intended solely for informational purposes and is presented without warranty of any kind. Swiftproxy does not guarantee the accuracy, completeness, or legal compliance of the information contained herein, nor does it assume any responsibility for content on thirdparty websites referenced in the blog. Prior to engaging in any web scraping or automated data collection activities, readers are strongly advised to consult with qualified legal counsel and to review the applicable terms of service of the target website. In certain cases, explicit authorization or a scraping permit may be required.
Join SwiftProxy Discord community Chat with SwiftProxy support via WhatsApp Chat with SwiftProxy support via Telegram
Chat with SwiftProxy support via Email