
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.
It depends on what you're doing.
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.
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.
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();
})();
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');
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();
})();
Install Puppeteer
npm install puppeteer
Import Puppeteer
const puppeteer = require('puppeteer');
Set a Custom User-Agent
await page.setUserAgent('Your_Custom_User_Agent');
Even with proper user-agent handling, you might still hit roadblocks. Here’s how to tackle them:
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.
Some sites reject invalid user-agent formats.
Solution: Use real browser user-agents from trusted sources.
Even with random user-agents, sending too many requests quickly can trigger restrictions.
Solution: Space out requests with setTimeout() and respect rate limits.
Some websites validate user-agents against known lists.
Solution: Use legitimate user-agent strings and update them regularly.
Libraries for generating user-agents may become outdated.
Solution: Regularly update dependencies and verify their functionality.
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.