User Agents

Configure Headless Chrome User Agent: Step-by-Step Guide

To configure the operating system user agent in Headless Chrome, you must pass the --user-agent command-line flag when launching the browser instance. This allows you to override the default "HeadlessChrome" identifier with a standard browser string, ensuring that websites recognize the browser as a legitimate desktop or mobile client rather than an automated script. This is essential for web scraping and automated testing where servers might otherwise block requests originating from known headless signatures.

What You'll Need

  • Google Chrome or Chromium installed on your system.
  • A programming environment (such as Node.js with Puppeteer, Python with Selenium, or a direct terminal/command prompt).
  • A valid, up-to-date User Agent string corresponding to the OS and browser version you wish to emulate.

Step-by-Step

  1. Identify your target User Agent string: Before launching the browser, determine which operating system and browser version you want to mimic. For example, a standard Windows 10 Chrome string looks like: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36. Using an outdated string can trigger bot detection systems.
  2. Configure the launch arguments: Depending on your tool, you will add the --user-agent flag to the browser arguments list.
    • Via Command Line: Add the flag directly after the executable path: google-chrome --headless --user-agent="Your User Agent String Here".
    • Via Puppeteer (Node.js): Use the args array in the puppeteer.launch() method:
      const browser = await puppeteer.launch({
              headless: true,
              args: ['--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36']
            });
    • Via Selenium (Python): Use the ChromeOptions class:
      from selenium import webdriver
      from selenium.webdriver.chrome.options import Options
      
      options = Options()
      options.add_argument('--headless')
      options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36')
      driver = webdriver.Chrome(options=options)
  3. Launch the browser instance: Execute your script or command. Chrome will now initialize and send the specified string in the User-Agent HTTP header for every request made during that session.
  4. Verify the configuration: To ensure the change is active, navigate to a "What is my User Agent" service or execute a script that prints the navigator.userAgent property via JavaScript:
    const userAgent = await page.evaluate(() => navigator.userAgent);
        console.log(userAgent);
    If the output matches your input string instead of containing "HeadlessChrome", the configuration is successful.

Common Mistakes

  • Using the wrong flag format: Some users attempt to set the user agent via a separate method call after the browser has already launched. The --user-agent flag must be passed at the moment of initialization to be fully effective across all browser contexts.
  • Forgetting quotes in CLI: When using the command line, User Agent strings contain spaces. If you do not wrap the entire string in double quotes, the OS will interpret the spaces as separators for new commands, leading to a launch failure.
  • Using an obsolete string: Using a User Agent from a browser version that is several years old is a red flag for modern security filters, which may lead to increased CAPTCHAs or immediate blocks.
  • Mismatching User Agent and Platform: Setting a Windows User Agent while the browser's navigator.platform still reports "Linux" (the actual OS of the server) can be detected by advanced scripts.

Tips

  • Rotate User Agents: If you are running a large number of requests, use a list of multiple real-world User Agent strings and rotate them randomly for each session to avoid pattern detection.
  • Match the Version: Ensure the Chrome version mentioned in your User Agent string closely matches the actual version of the Headless Chrome binary you are using.
  • Combine with other headers: For better emulation, consider setting the Accept-Language and Sec-Ch-Ua headers to match the persona of the User Agent you have chosen.

Troubleshooting

If the website still detects that you are using a headless browser despite changing the User Agent, consider the following solutions:

  • Check for "HeadlessChrome" in the string: Ensure you haven't accidentally left the default string in your configuration. The presence of the word "Headless" is the primary trigger for most blocks.
  • Inspect WebDriver flags: Many sites check the navigator.webdriver property, which is set to true in headless mode. To fix this, you may need to use a plugin like puppeteer-extra-plugin-stealth or execute a script to overwrite the property: Object.defineProperty(navigator, 'webdriver', {get: () => undefined}).
  • Verify HTTP Header precedence: In some frameworks, setting the user agent via the browser launch arguments is overridden by a later page.setUserAgent() call. Check your code for redundant or conflicting calls.

Frequently Asked Questions

Does changing the User Agent make Headless Chrome invisible?

No. While it hides the most obvious identifier, websites can still detect headless mode through other means, such as the navigator.webdriver property, missing browser plugins, or specific CSS/JS execution timings.

Can I use a mobile User Agent in Headless Chrome?

Yes. By passing a mobile string (e.g., an iPhone or Android string), the server will serve the mobile version of the site. However, you should also set the viewport size to match a mobile screen for a consistent experience.

Is it legal to change the User Agent?

Changing a User Agent is a technical configuration of your own software. However, you must always comply with a website's Terms of Service and robots.txt file when automating requests.

Do I need to restart the browser to change the User Agent?

Yes, if you are using the --user-agent launch flag, the browser must be restarted. If you are using a library like Puppeteer that provides a setUserAgent() method, you can change it per page, but the launch flag is generally more robust.

Why does my User Agent change back to default?

This usually happens if you are using a browser manager or a cloud-based grid that overrides launch arguments with its own default configuration. Check your environment settings to ensure custom arguments are permitted.