- 1 year ago
How to set up language in Puppeteer?
To set up the language in Puppeteer, you can use the page.setExtraHTTPHeaders
method to set the Accept-Language
header, which specifies the preferred language for the browser. Here's an example of how you can do it:
javascriptconst puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Set the desired language
const language = 'en-US'; // Replace with the desired language code
// Set the Accept-Language header
await page.setExtraHTTPHeaders({ 'Accept-Language': language });
// Perform your Puppeteer actions
// ...
await browser.close();
})();
In the example above, Puppeteer is launched, a new page is created, and the Accept-Language
header is set to 'en-US'
, indicating English (United States) as the preferred language. Replace 'en-US'
with the desired language code, such as 'en-GB'
for English (United Kingdom) or 'fr'
for French.
By setting the Accept-Language
header, Puppeteer instructs the browser to use the specified language for content negotiation when accessing websites. This can influence the language used for text localization and localization-dependent features on websites.
Remember to place your Puppeteer actions within the async function to ensure proper asynchronous execution.