The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Set userAgent when you create a browser context. In a Playwright library script, use browser.newContext({ userAgent: '...' }); in Playwright Test, configure it with test.use({ userAgent: '...' }). Set the value before creating pages in that context.
Set a user agent in a Playwright script
Pass the user-agent string as the userAgent option to browser.newContext(). This option is configured on the context, so create the page from that context after setting it.
As an Amazon Associate I earn from qualifying purchases.
import { chromium } from 'playwright';
const browser = await chromium.launch();
const context = await browser.newContext({
userAgent: 'My user agent',
});
const page = await context.newPage();
await page.goto('https://example.com');
console.log(await page.title());
await browser.close();
Replace My user agent with the exact value needed for your test. The BrowserType API documents userAgent as an optional context setting: BrowserType API.
Set it in Playwright Test
For Playwright Test, configure the option with test.use():
#1 Best Overall
import { test, expect } from '@playwright/test';
test.use({ userAgent: 'My user agent' });
test('loads the page with the configured user agent', async ({ page }) => {
await page.goto('https://example.com');
await expect(page).toHaveTitle(/Example Domain/);
});
This applies the configured context option to the test. See the official Playwright emulation guide for both the context and test configuration approaches.
Choose between an override and device emulation
| Approach | Use it for | What it configures |
|---|---|---|
browser.newContext({ userAgent: '...' }) |
Playwright library scripts | The context’s user-agent value. |
test.use({ userAgent: '...' }) |
Playwright Test | The user-agent value for the test context. |
| Device descriptor | Broader device emulation | A device configuration that includes a user agent. |
Playwright notes that the user agent is included in device descriptors and is rarely something users need to change. If you only need a different user-agent value, use the override; if your test needs broader device emulation, consider a device descriptor. Changing the string alone should not be treated as reproducing every browser or device characteristic. See the BrowserContext API.
Rank #2
Common mistakes and fixes
- Setting the option after creating the page: configure
userAgentwhen creating the context, then create pages from that context. - Changing the string but expecting full device emulation: the option sets the user-agent value; use a device descriptor when the goal is broader device emulation.
- Using the library pattern in a Playwright Test configuration: use
test.use({ userAgent: '...' })for Playwright Test, orbrowser.newContext({ userAgent: '...' })in a library script.
Or skip the browser setup
If your goal is to capture a website rather than run a Playwright test, ScreenshotNeo can return a screenshot or PDF from one GET request. It accepts cookie and consent banners before capture and removes 60+ known consent platforms, newsletter popups, and chat widgets; these steps can each be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, with the outcome identified in response headers. Its MCP server lets AI agents use screenshot and PDF tools. The free plan includes 1,000 shots a month with no card; paid plans start at $5 for 3,000.
Install nothing for this call; replace the URL with the page you want to capture and supply your API key:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
See the ScreenshotNeo API documentation for request options. Sign up for 1,000 free screenshots a month, with no card required.
Rank #3
Frequently Asked Questions
Does setting `userAgent` affect every browser context?
No. Set the option on each context that needs the custom value.
Is a custom user agent required for most Playwright tests?
Playwright says it is rarely necessary because device descriptors include a user agent.
Recommended Free Tools
Quick Recap
Best Value
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




