User Agents

Web browsers and everyday web tech, explained

Set Language in Selenium Headless Chrome: Step-by-Step

· 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

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

  1. Import required modules. In your Python script, import webdriver and Options from selenium.
  2. Create a Chrome Options instance. Instantiate webdriver.ChromeOptions().
  3. Add the --lang argument. Use options.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.
  4. Enable headless mode. Add options.add_argument('--headless') (or options.headless = True in some versions).
  5. 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.
  6. Initialize the driver. Create driver = webdriver.Chrome(options=options).
  7. Verify the language. Navigate to a page like https://www.wikipedia.org or a site that redirects based on language, and check the page's lang attribute or visible text.
  8. 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, not pt_BR). Underscores are not recognized.
  • Forgetting to set both --lang and intl.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=new is 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=AutomationControlled to 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.language property 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 --version and google-chrome --version to compare.
  • Headless mode shows blank page. Add --no-sandbox and --disable-dev-shm-usage if 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.