User Agents

Web browsers and everyday web tech, explained

Change Headless Chrome User Agent: A Complete Guide

· 4 min read · How the Web Works

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Changing the user agent in headless Chrome is achieved by passing a specific command-line argument, --user-agent, when launching the browser instance. This process modifies the identification string that Chrome sends to web servers, allowing the browser to mimic different devices, operating systems, or browser versions. This is primarily used by developers to test how a website renders across different platforms or to ensure that a server serves the correct version of a page to a specific client type without needing a physical device for every test case.

What You'll Need

  • A computer with Node.js installed (if using Puppeteer) or Python installed (if using Selenium).
  • The Chrome browser or Chromium installed on your system.
  • A library to control the browser, such as Puppeteer, Playwright, or Selenium WebDriver.
  • A valid user agent string for the device or browser you wish to emulate.

Step-by-Step

  1. Identify the Target User Agent: Find the exact user agent string you want to use. For example, if you want to mimic a recent version of Chrome on Windows 10, you would use a string like Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36.
  2. Configure the Launch Arguments: Depending on your automation framework, you must add the user agent to the browser launch options.
  3. Implementation via Puppeteer (Node.js):
    • Import Puppeteer into your script.
    • In the puppeteer.launch() method, add the args array.
    • Include '--user-agent=YOUR_USER_AGENT_STRING' within that array.
    • Alternatively, use page.setUserAgent('YOUR_USER_AGENT_STRING') after the page object has been created to apply the change to a specific tab.
  4. Implementation via Selenium (Python):
    • Import Options from selenium.webdriver.chrome.options.
    • Create an instance of Options().
    • Use the add_argument() method: options.add_argument('--user-agent=YOUR_USER_AGENT_STRING').
    • Pass the options object into the WebDriver initialization: webdriver.Chrome(options=options).
  5. Enable Headless Mode: Ensure the --headless flag is also present in your arguments to ensure the browser runs without a visible GUI.
  6. Verify the Change: Navigate the browser to a service like whatsmyuseragent.org or execute a script to print navigator.userAgent from the browser console to confirm the string has been updated.

Common Mistakes

  • Incorrect String Formatting: Forgetting the equals sign in --user-agent=string or failing to wrap the entire argument in quotes can cause the browser to ignore the setting or fail to launch.
  • Mismatching Window Size: Changing the user agent to a mobile device without also changing the viewport size. A server may see a mobile user agent but a desktop resolution, which can lead to inconsistent rendering or detection.
  • Using Outdated Strings: Using a user agent from a browser version that is several years old. Some modern websites may serve legacy code or trigger security warnings when they detect an obsolete browser version.
  • Applying the Agent Too Late: Setting the user agent after the first request has already been sent to the server. This means the initial handshake uses the default headless Chrome agent, and only subsequent requests use the custom one.

Tips

  • Use a Rotation List: If you are performing extensive testing across multiple platforms, create an array of common user agent strings and randomly select one for each session to ensure broad coverage.
  • Combine with Viewport Settings: When emulating a mobile device, always use the setViewport method (in Puppeteer) or set_window_size (in Selenium) to match the screen dimensions of the device associated with your user agent.
  • Check for "Headless" Indicators: Be aware that some websites check for more than just the user agent. They may check for the navigator.webdriver property, which is set to true in automated environments regardless of the user agent.
  • Keep Strings Current: Periodically update your list of user agents to match the current stable releases of Chrome, Firefox, and Safari.

Troubleshooting

If the user agent does not appear to be changing, first check the console logs for any launch errors. Ensure that you are not overriding the user agent later in your code with a different command. If you are using a proxy, ensure the proxy is not stripping or modifying the headers before they reach the destination server.

If the website still identifies the browser as "headless," it is likely because the site is using navigator.webdriver or checking for specific Chrome-only properties that are missing in headless mode. In these cases, you may need to use a plugin like puppeteer-extra-plugin-stealth to further mask the automation signatures.

Frequently Asked Questions

Does changing the user agent make headless Chrome undetectable?

No. The user agent is only one of many signals. Websites can also detect automation via the navigator.webdriver flag, CSS media queries, and the absence of certain browser plugins.

Can I change the user agent for only one specific tab?

Yes, in frameworks like Puppeteer, you can use page.setUserAgent() on a specific page object rather than setting it globally in the launch arguments.

Which is better: launch arguments or page-level settings?

Launch arguments are generally more robust as they apply to the entire browser process from the start. Page-level settings are more flexible for multi-tab scenarios.

Will a custom user agent affect page load speed?

Generally, no. However, if you use a mobile user agent, the server may send a smaller, mobile-optimized version of the page, which could actually load faster.

Do I need to restart the browser for the user agent to take effect?

If you set the user agent via launch arguments, the browser must be started with those settings. If you use a page-level method, it applies to that specific page instance immediately.