User Agents

Web browsers and everyday web tech, explained

Set Accept-Language Header in Headless Chrome

· 4 min read · Browsers

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

To set the Accept-Language header in Headless Chrome, you must configure the browser's language preferences via launch arguments or the Chrome DevTools Protocol (CDP). Setting this header allows you to simulate users from different regions, ensuring that the website serves the correct localized content, currency, and language based on the browser's reported preferences. This is critical for automated testing of internationalized sites and scraping content that varies by locale.

What You'll Need

  • A Node.js environment (if using Puppeteer or Playwright).
  • A version of Chrome or Chromium installed.
  • A library to control Chrome, such as Puppeteer, Playwright, or Selenium.
  • The specific BCP 47 language tag you wish to simulate (e.g., en-US, fr-FR, es-ES).

Step-by-Step

  1. Identify the Target Locale: Determine the language code you need. For example, if you want the page to render in German, use de-DE. If you want a fallback list, prepare a comma-separated string like fr-CH, fr;q=0.9, en;q=0.8.
  2. Configure Launch Arguments: The most reliable way to set the language in Headless Chrome is through the --lang flag during the browser launch process. This flag sets the navigator.language property and the Accept-Language request header simultaneously.
  3. Implement in Puppeteer: When calling puppeteer.launch(), add the --lang flag to the args array.
    const browser = await puppeteer.launch({ args: ['--lang=es-ES'] });
  4. Implement in Playwright: In Playwright, the language is set within the browser context options rather than as a launch flag.
    const context = await browser.newContext({ locale: 'de-DE' });
  5. Using the Chrome DevTools Protocol (CDP): If the browser is already running and you need to change the language dynamically, use the Emulation.setLocaleOverride command via CDP. This allows you to switch locales without restarting the browser instance.
  6. Verify the Header: To ensure the setting is active, navigate to a site like httpbin.org/headers or execute a script in the console to check navigator.language.

Common Mistakes

  • Confusing Locale with Language: Using a general language code (e.g., en) when the site requires a specific regional locale (e.g., en-GB) can result in the site defaulting to a generic version rather than the regional one.
  • Overlooking the Browser Context: In Playwright, setting the language at the browser launch level doesn't always propagate to every context. Always set the locale within the newContext call.
  • Ignoring Header Precedence: Some websites ignore the Accept-Language header if they detect a different location via the IP address (GeoIP). Setting the header alone may not be enough if the site uses server-side IP tracking.
  • Incorrect Flag Syntax: Using -language instead of --lang in the Chrome arguments will result in the flag being ignored.

Tips

  • Use Multiple Languages: You can specify a preference order by providing multiple languages. While the --lang flag typically takes one primary value, you can use CDP to set a more complex Accept-Language string to test how the site handles fallback languages.
  • Combine with User-Agent: For maximum authenticity, ensure your User-Agent string matches the locale you are simulating. A German language header paired with a US-English User-Agent can look suspicious to some bot-detection systems.
  • Automate Locale Testing: Create an array of locales (e.g., ['en-US', 'fr-FR', 'ja-JP']) and loop through them to run your test suite across all supported languages in a single execution.

Troubleshooting

If the page is still loading in the default language despite your settings, try the following:

  • Clear Cache and Cookies: Websites often store the user's language preference in a cookie. If you are reusing a browser profile, clear the cookies before setting the new locale.
  • Check for Hardcoded Overrides: Some sites use JavaScript to detect the language via navigator.languages (plural). Ensure you are using a tool that overrides both the single language and the languages array.
  • Test with a Non-Headless Window: Run the browser in headful mode (headless: false) and open the Network tab in DevTools. Inspect the request headers of the initial document request to see if Accept-Language is actually being sent.
  • Verify BCP 47 Compliance: Ensure your language tags are formatted correctly. An invalid tag like English-US will be ignored; it must be en-US.

Frequently Asked Questions

Does the --lang flag change the Accept-Language header?

Yes, in Headless Chrome, the --lang flag sets both the navigator.language JavaScript property and the HTTP Accept-Language request header.

Can I change the language without restarting the browser?

Yes, by using the Chrome DevTools Protocol (CDP) and the Emulation.setLocaleOverride method, you can change the locale for the current session without a full restart.

Why does the site still show English even after setting the locale to French?

This usually happens because the site is using GeoIP tracking based on your IP address, which overrides the browser's language header. You would need a proxy or VPN to match the IP to the locale.

Is there a difference between locale and language in Playwright?

In Playwright, the locale option handles both. Setting locale: 'fr-FR' tells the browser to report that the user's preferred language is French as spoken in France.

How do I set multiple preferred languages?

While the --lang flag is typically used for a single primary language, you can use CDP to set a custom Accept-Language header string containing weighted preferences (q-factors).