Set Language in Selenium Headless Chrome: Step-by-Step
In this article
Setting the language in Selenium headless Chrome ensures that your automated tests or scrapers interact with web pages in a specific locale, which is crucial for verifying localized content, testing multi-language sites, or avoiding default English fallbacks. This guide shows you how to configure Chrome's language preference when running in headless mode, using Selenium WebDriver with Python (though the same options apply to Java, JavaScript, and other bindings). You'll need Selenium installed and a compatible ChromeDriver version.
What You'll Need
- Selenium WebDriver installed (e.g.,
pip install selenium) - ChromeDriver matching your Chrome version, accessible in PATH or specified explicitly
- Basic familiarity with writing Selenium scripts
Step-by-Step
- Import required modules. In your Python script, import
webdriverandOptionsfromselenium. - Create a Chrome Options instance. Instantiate
webdriver.ChromeOptions(). - Add the
--langargument. Useoptions.add_argument('--lang=fr-FR')(replace with your desired locale code, e.g.,en-US,de-DE,ja-JP). This sets the browser's UI and accepted languages. - Enable headless mode. Add
options.add_argument('--headless')(oroptions.headless = Truein some versions). - Set prefs for language (optional but recommended). Use
options.add_experimental_option('prefs', {'intl.accept_languages': 'fr-FR'})to reinforce the language for content negotiation. - Initialize the driver. Create
driver = webdriver.Chrome(options=options). - Verify the language. Navigate to a page like
https://www.wikipedia.orgor a site that redirects based on language, and check the page'slangattribute or visible text. - Close the driver. After tests, call
driver.quit().
Common Mistakes
- Using wrong locale codes. Ensure you use valid BCP 47 codes (e.g.,
pt-BR, notpt_BR). Underscores are not recognized. - Forgetting to set both
--langandintl.accept_languages. Some sites rely on the Accept-Language header, which is controlled by the pref, not just the UI language. - Placing arguments after driver initialization. All options must be set before creating the WebDriver instance.
- Using deprecated headless mode syntax. In newer Chrome versions,
--headless=newis recommended for better compatibility with certain features. - Not clearing browser cache. If you've run tests before, Chrome may cache the old language; use a fresh profile or clear cache.
Tips
- Use
--disable-blink-features=AutomationControlledto reduce detection if you're scraping, though it's not required for language. - For multi-language testing, create a helper function that takes a locale as a parameter and returns a configured driver.
- Check the
navigator.languageproperty in the browser console to confirm the language is set. - If you need to test multiple languages in one session, you can update the prefs and restart the driver, but it's simpler to create a new driver per language.
- For Java or JavaScript, the same options are available—just follow the binding's syntax.
Troubleshooting
- Language not applied. Verify that the site actually respects the Accept-Language header. Some sites use IP-based geolocation. Try a different site known to localize, like Google or Wikipedia.
- ChromeDriver fails to start. Ensure ChromeDriver version matches your installed Chrome browser. Run
chromedriver --versionandgoogle-chrome --versionto compare. - Headless mode shows blank page. Add
--no-sandboxand--disable-dev-shm-usageif running in a container or CI environment. - Prefs not taking effect. Clear the user data directory or set a unique one via
options.add_argument('--user-data-dir=/tmp/chrome-profile')to avoid conflicts. - Language changes but only partially. Some pages load content dynamically via JavaScript; wait for the page to fully render using explicit waits.
Frequently Asked Questions
Does setting --lang affect only the browser UI, not the page content?
It affects both the UI and the Accept-Language header, but page content depends on the site's logic. Many sites use the header to serve localized content.
Can I set multiple languages?
Yes, you can list them in order of preference, e.g., --lang=fr-FR,fr,en-US. The browser will use the first available.
Is this the same as setting the OS locale?
No, this is browser-specific. The OS locale doesn't affect Chrome's language unless you also set it in Chrome's settings.
Will this work with headless Chrome on all operating systems?
Yes, the options are cross-platform, but you may need to adjust paths for ChromeDriver on Windows/macOS/Linux.
How do I verify the language is set correctly?
Use JavaScript execution: driver.execute_script('return navigator.language') should return the locale you set.
What if the site still shows English?
Check if the site uses geolocation or has a language selector cookie. You may need to set cookies or use a proxy with a different IP, but that's beyond the scope of this guide.