Set Accept-Language Header in Headless Chrome
In this article
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
- 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 likefr-CH, fr;q=0.9, en;q=0.8. - Configure Launch Arguments: The most reliable way to set the language in Headless Chrome is through the
--langflag during the browser launch process. This flag sets thenavigator.languageproperty and theAccept-Languagerequest header simultaneously. - Implement in Puppeteer: When calling
puppeteer.launch(), add the--langflag to theargsarray.const browser = await puppeteer.launch({ args: ['--lang=es-ES'] }); - 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' }); - Using the Chrome DevTools Protocol (CDP): If the browser is already running and you need to change the language dynamically, use the
Emulation.setLocaleOverridecommand via CDP. This allows you to switch locales without restarting the browser instance. - Verify the Header: To ensure the setting is active, navigate to a site like
httpbin.org/headersor execute a script in the console to checknavigator.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
localewithin thenewContextcall. - Ignoring Header Precedence: Some websites ignore the
Accept-Languageheader 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
-languageinstead of--langin 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
--langflag typically takes one primary value, you can use CDP to set a more complexAccept-Languagestring 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 singlelanguageand thelanguagesarray. - 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 ifAccept-Languageis actually being sent. - Verify BCP 47 Compliance: Ensure your language tags are formatted correctly. An invalid tag like
English-USwill be ignored; it must been-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).