如何在Puppeteer中指定浏览器语言 [英] How to specify browser language in Puppeteer
问题描述
我想使用Puppeteer推出一款语言西班牙语 es
的Google Chrome浏览器。
I would like to launch a Google Chrome browser with language Spanish es
using Puppeteer.
我是试过 puppeteer.launch(args:[' - lang = es',...],...)
但它没有用。
我试过传递环境变量 LANGUAGE = es mocha puppeteer-test.js
但它没有用。
I've tried passing the environment variable LANGUAGE=es mocha puppeteer-test.js
but it didn't work.
我尝试使用 userDataDir
选项并传递一个带有偏好设置的文件夹
提交 {intl:{accept_languages:es}}
但浏览器设置 - 语言仍然不显示西班牙语,也不显示 window.navigator.languages
既不 window.navigator.language
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
我正在使用
Puppeteer 0.11.0
节点8.4.0
NPM 5.2.0
macOS El Capitan 10.11.6
MacBook Pro Retina,15英寸,2015年中期
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
推荐答案
有几种方法可以更改区域设置,您可以尝试所有o如果他们找到适合你的东西,
There are several ways to change locale, you can try all of them to find what works for you,
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"];
}
});
});
为了测试,我将用多种语言测试,包括 es
,结果如下。
For the sake of testing, I'll test this in multiple languages, including es
, and here is the result.
< a href =https://i.stack.imgur.com/nopL4.png =noreferrer>
这篇关于如何在Puppeteer中指定浏览器语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!