Set Accepted Language for Chrome Headless with Selenium in Python
In this article
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_managerto auto-manage it)
Step-by-Step
- Import Selenium and set up Chrome options.
Start your Python script with
from selenium import webdriverandfrom selenium.webdriver.chrome.options import Options. - Create an Options object.
options = Options() - Add the headless argument.
options.add_argument('--headless')(or--headless=newfor newer Chrome versions). - Set the
--langargument.options.add_argument('--lang=fr')— replacefrwith your desired language code (e.g.,de,es,ja). This sets the browser's UI language and theAccept-Languageheader for most requests. - 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 .
- Initialize the driver with your options.
driver = webdriver.Chrome(options=options) - Navigate to a test page to verify.
Go to a site that reflects language, like
https://www.wikipedia.orgor a localized page, and check the content or theAccept-Languageheader via a service likehttpbin.org/headers. - Close the driver when done.
driver.quit()
Common Mistakes
- Using only
--langbut not setting the header for all requests. Some sites ignore the--langflag and rely solely on the HTTP header. Use the CDP method to be safe. - Forgetting to include both the language and region. Use
fr-FRinstead of justfrif you need a specific dialect. - Setting the language after the driver starts. The
--langargument 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/headersto see exactly what your browser sends. - Combine methods. Use both
--langand 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.