User Agents

Web browsers and everyday web tech, explained

Set Accepted Language for Chrome Headless with Selenium in Python

· 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

When you run Chrome in headless mode with Selenium, the browser often sends a default Accept-Language header (usually en-US,en;q=0.9). If you need to test a site in a specific language—or ensure your scraper gets localized content—you must explicitly set the accepted language. This guide shows you how to do that in Python, using Chrome options and, if needed, a small tweak to the Chrome binary.

What You'll Need

  • Python 3.6+ installed
  • Selenium library installed (pip install selenium)
  • Chrome browser installed (even if you use headless, Chrome must be present)
  • ChromeDriver matching your Chrome version (or use webdriver_manager to auto-manage it)

Step-by-Step

  1. Import Selenium and set up Chrome options. Start your Python script with from selenium import webdriver and from selenium.webdriver.chrome.options import Options.
  2. Create an Options object. options = Options()
  3. Add the headless argument. options.add_argument('--headless') (or --headless=new for newer Chrome versions).
  4. Set the --lang argument. options.add_argument('--lang=fr') — replace fr with your desired language code (e.g., de, es, ja). This sets the browser's UI language and the Accept-Language header for most requests.
  5. Add the Accept-Language header via Chrome DevTools Protocol (CDP) if needed. For more precise control, use options.add_experimental_option('prefs', {'intl.accept_languages': 'fr,fr_FR'}) or set the header directly after launching the driver using .
  6. Initialize the driver with your options. driver = webdriver.Chrome(options=options)
  7. Navigate to a test page to verify. Go to a site that reflects language, like https://www.wikipedia.org or a localized page, and check the content or the Accept-Language header via a service like httpbin.org/headers.
  8. Close the driver when done. driver.quit()

Common Mistakes

  • Using only --lang but not setting the header for all requests. Some sites ignore the --lang flag and rely solely on the HTTP header. Use the CDP method to be safe.
  • Forgetting to include both the language and region. Use fr-FR instead of just fr if you need a specific dialect.
  • Setting the language after the driver starts. The --lang argument must be set before initialization; changing it later won't affect the session.
  • Using an unsupported language code. Ensure the code is valid (e.g., en-US, pt-BR). Invalid codes may be ignored.

Tips

  • Test with a header echo service. Visit httpbin.org/headers to see exactly what your browser sends.
  • Combine methods. Use both --lang and the CDP header for maximum compatibility.
  • Use a user-agent that matches the language. Some sites detect language from the user-agent; you can set a custom one via options.add_argument('--user-agent=...').
  • For dynamic language switching, create a new driver instance. Each driver session has a fixed language; if you need multiple languages, instantiate a new driver per language.

Troubleshooting

Language doesn't change on the site. First, verify the header by navigating to httpbin.org/headers. If it shows the default, your CDP command may have run before the page loaded—move it after driver.get() but before interacting with the page. Also, ensure you're using execute_cdp_cmd correctly.

ChromeDriver fails to start. If you get a session-not-created error, check that ChromeDriver version matches your Chrome version. Use webdriver-manager to avoid manual downloads: pip install webdriver-manager and then from webdriver_manager.chrome import ChromeDriverManager with webdriver.Chrome(ChromeDriverManager().install(), options=options).

Headless mode ignores --lang. In some Chrome versions, the headless mode has a separate flag. Try --headless=new or use the CDP method, which works regardless of headless state.

The site still shows English. Some sites detect language via JavaScript or cookies, not the header. In that case, you may need to set a cookie (e.g., driver.add_cookie({'name': 'lang', 'value': 'fr'})) after navigating to the domain, then reload.

Frequently Asked Questions

Does --lang affect the Accept-Language header?

Yes, in most Chrome versions, the --lang argument sets both the UI language and the Accept-Language header for the initial request. However, some sites may still receive the default header if the request is made before the flag takes effect—using CDP is more reliable.

Can I set multiple languages?

Yes. Use a comma-separated list with quality values, e.g., 'fr-FR,fr;q=0.9,en;q=0.8'. In the CDP command, set the header accordingly.

Will setting the language slow down my script?

No, it has negligible performance impact. The only overhead is the extra CDP command, which is milliseconds.

Does this work with Chrome for Testing or Chromium?

Yes, the same options work with any Chromium-based browser. Just ensure you have the correct driver.

Why does my site still show English after setting the header?

Some sites use geo-IP or browser fingerprinting instead of the header. Try using a VPN or proxy (not covered here) or set a cookie that the site expects for language preference.

Can I change the language mid-session?

No, the Accept-Language header is fixed per session. To change it, you must create a new WebDriver instance with the new settings.