本文介绍了如何在Puppeteer中指定浏览器语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想使用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.languageI'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 usingPuppeteer 0.11.0Node 8.4.0NPM 5.2.0macOS El Capitan 10.11.6MacBook 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 Headerawait page.setExtraHTTPHeaders({ 'Accept-Language': 'bn'}); 强行设置语言Forcefully set the language// Set the language forcefully on javascriptawait 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. 这篇关于如何在Puppeteer中指定浏览器语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-11 00:21