DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

How to Set a Custom User Agent in Playwright

Set a custom user agent when creating a Playwright browser context, or configure it with test.use() in Playwright Test. Learn when device emulation is a better fit.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set it in Playwright Test

For Playwright Test, configure the option with test.use():

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.

Common mistakes and fixes

  • Setting the option after creating the page: configure userAgent when 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, or browser.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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.