Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify browser language in Puppeteer

Tags:

I would like to launch a Google Chrome browser with language Spanish es using Puppeteer.

I've tried puppeteer.launch(args:['--lang=es',...],...) but it didn't work.

I've tried passing the environment variable LANGUAGE=es mocha puppeteer-test.js but it didn't work.

I've tried using the userDataDir option and passing a folder with a Preferences file a { "intl": { "accept_languages": "es" } } but the browser Settings - Languages still don't show Spanish and neither does window.navigator.languages neither window.navigator.language

I'm using
Puppeteer 0.11.0
Node 8.4.0
NPM 5.2.0
macOS El Capitan 10.11.6
MacBook Pro Retina, 15-inch, Mid 2015

like image 580
Giorgio Avatar asked Oct 24 '17 10:10

Giorgio


People also ask

Can puppeteer use Chrome instead of Chromium?

Puppeteer is a Node library which provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol. It can also be configured to use full (non-headless) Chrome or Chromium. An explanation of what Puppeteer is and the things it can do.

Can I use puppeteer in browser?

Puppeteer lets you automate the testing of your web applications. With it, you can run tests in the browser and then see the results in real-time on your terminal. Puppeteer uses the WebDriver protocol to connect with the browser and simulate user interaction with HTML elements or pages.

Is puppeteer only for Chrome?

It supports:Chrome/Chromium (+ Edge, Opera, Chromium-based browsers) Firefox. Webkit (Safari)

Does puppeteer install Chromium?

By default, when you install Puppeteer, the installer downloads a recent version of Chromium, the open-source browser that Microsoft Edge is also built upon.


2 Answers

There are several ways to change locale, you can try all of them to find what works for you,

Use Args when launching

const browser = await puppeteer.launch({     headless: false,     args: ['--lang=bn-BD,bn'] }); 

Send the language as Header

await page.setExtraHTTPHeaders({     'Accept-Language': 'bn' }); 

Forcefully set the language

// Set the language forcefully on javascript await page.evaluateOnNewDocument(() => {     Object.defineProperty(navigator, "language", {         get: function() {             return "bn-BD";         }     });     Object.defineProperty(navigator, "languages", {         get: function() {             return ["bn-BD", "bn"];         }     }); }); 

For the sake of testing, I'll test this in multiple languages, including es, and here is the result.

Google search:

es bn

BrowserLeaks:

enter image description here

like image 79
Md. Abu Taher Avatar answered Sep 19 '22 01:09

Md. Abu Taher


There's an error in Md-Abu-Taher's answer.

The response to navigator.language should return a string, not an array. Try it in your own browser console.

The code snippet should be:

await page.evaluateOnNewDocument(() => {     Object.defineProperty(navigator, "language", {         get: function() {             return "en-GB";         }     });     Object.defineProperty(navigator, "languages", {         get: function() {             return ["en-GB", "en"];         }     }); }); 
like image 31
kaitlinsm Avatar answered Sep 17 '22 01:09

kaitlinsm