![]()
Playwright: Using Fake User Agents
User agents are strings that identify the browser and device making requests to websites. When automating browsers with Playwright, using custom or fake user agents can be useful for bypassing bot detection and accessing user agent-specific content.
This guide covers several methods for setting and managing fake user agents with Playwright.
- How To Use Fake User-Agents In Playwright
- What Are Fake User-Agents?
- User-Agents & Anti-Bots
- How Playwright Manages User-Agents
- Use Random User-Agent for Each Session
- Use Playwright Stealth Plugin
- Obtaining User-Agent Strings
- Troubleshooting Guide
- Conclusion
- More Web Scraping Guides
Need help scraping the web?
Then check out ScrapeOps, the complete toolkit for web scraping.
How To Use Fake User-Agents In Playwright
Here is a simple example of directly setting a fake user agent string when launching a Playwright browser context:
const context = await browser.newContext({
userAgent:
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
});
This approach allows complete control over the user agent value. However, the drawback is you have to manually manage a pool of strings. Later sections cover more robust and automated methods.