I’m trying to start a Chrome browser with Puppeteer and set the language to Spanish. I’ve tried a few things:
- Using
puppeteer.launch(args:['--lang=es'])
- Setting the
LANGUAGE
environment variable
- Using
userDataDir
with a Preferences
file
None of these worked. The browser settings don’t show Spanish and navigator.languages
and navigator.language
don’t reflect the change either.
Here’s a simple code example that didn’t work:
const browser = await puppeteer.launch({
args: ['--lang=es'],
userDataDir: './custom-profile'
});
const page = await browser.newPage();
await page.goto('https://example.com');
const lang = await page.evaluate(() => navigator.language);
console.log(lang); // Still shows 'en-US' instead of 'es'
Any ideas on how to properly set the browser language? I’m using Puppeteer 0.11.0 and Node 8.4.0 on macOS El Capitan.
Have you considered using the --lang
flag in combination with the defaultViewport
option? This approach worked for me in a similar scenario. Here’s a code snippet that might help:
const browser = await puppeteer.launch({
args: ['--lang=es'],
defaultViewport: {
width: 1280,
height: 720
}
});
const page = await browser.newPage();
await page.setExtraHTTPHeaders({
'Accept-Language': 'es'
});
This setup not only sets the browser language but also ensures the ‘Accept-Language’ header is properly set for requests. Additionally, you might want to check if your Puppeteer version is up to date, as older versions sometimes have issues with language settings.
If you’re still facing problems, consider clearing the browser cache and cookies before each run to ensure a clean state. Let me know if this works for you!
hey mate, have u tried settin the ‘intl.accept_languages’ preference? it worked for me. heres the code:
const browser = await puppeteer.launch({
args: [‘–lang=es’],
defaultPreferences: {‘intl.accept_languages’: ‘es’}
});
this should do the trick. let me kno if it helps!
I’ve encountered this issue before, and I found that the most reliable way to set the language in Puppeteer is by using the --lang
argument along with the extraPrefsFirefox
option. Here’s what worked for me:
const browser = await puppeteer.launch({
args: ['--lang=es'],
extraPrefsFirefox: {
'intl.accept_languages': 'es'
}
});
This approach ensures that both the browser UI and the navigator.language
are set correctly. Keep in mind that some websites might still detect your location based on IP and adjust content accordingly.
Also, make sure you’re using a recent version of Puppeteer. I noticed you’re on 0.11.0, which is quite old. Upgrading to the latest version might resolve some issues you’re experiencing.
If you’re still having trouble, you could try setting the language preferences directly in the page context:
await page.evaluateOnNewDocument(() => {
Object.defineProperty(navigator, 'language', {
get: function() {
return 'es';
}
});
Object.defineProperty(navigator, 'languages', {
get: function() {
return ['es', 'es-ES'];
}
});
});
Hope this helps!