How to pass experimental chrome options in puppeteer How to pass experimental chrome options in puppeteer selenium selenium

How to pass experimental chrome options in puppeteer


I.) Normally you should be able to set any experimental features among the args if you are able to position them between the --flag-switches-begin and --flag-switches-end flags and you give them in the following format: Pascal case option names, separated by a single comma without a space.E.g. if you want to set enable-nacl and network-service:

--enable-features=EnableNacl,NetworkService

Experimental features can be checked here: chrome://flags if they are indeed enabled.

The Command Line of Chrome/Chromium can be checked here: chrome://version to see if the flags are added at the launch.


II.) For me this solution haven't worked (it doesn't mean it wouldn't work for you, it worths a try). So I use a workaround of creating a Chrome profile (Experimental Profile 1) where I enable manually the experimantal flags and then using this profile with puppeteer (--user-data-dir= and --profile-directory= are required):

const puppeteer = require('puppeteer')async function fn() {  const browser = await puppeteer.launch({    headless: false,    args: [      // '--enable-features=EnableNacl,NetworkService',      '--user-data-dir=C:\\Users\\user.name\\AppData\\Local\\Chromium\\User Data',      '--profile-directory=Experimental Profile 1'    ]  })  const page = await browser.newPage()  await page.goto('chrome://flags/#enable-nacl')}fn()

The example above is for Windows, see more platforms and more info here.

Note: You can retrieve executable and profile paths on the chrome://version page.

Note 2: The example above can be run only with headless: false as headless mode cannot visit invalid URLs like chrome://flags/#enable-nacl but the user profile should work. If it isn't then there is a known chromium bug related to relative path of data dir which be solved with path.resolve, e.g.:

const path = require('path')const browser = await puppeteer.launch({ headless: true, args: [`--user-data-dir=${path.resolve(__dirname, 'User Data') }`] })

[source]


Edit

Actually the options you want to set are only called experimental options by ChromeDriver but available as chrome policies at chrome://policy page. Make sure you've set Show policies with no value set to see them.

But as far as I see it doesn't provide a simple solution (e.g. in case of Windows you are limited to set it via registry). After searching on GitHub I've found in this discussion that chrome_options.add_experimental_option is not implemented in puppeteer as in ChromeDriver.But in puppeteer-extra there is a dedicated plugin for this, called: user-preferences.

For example:

const puppeteer = require('puppeteer-extra')puppeteer.use(require('puppeteer-extra-plugin-user-preferences')({ userPrefs: {  download: {    prompt_for_download: false  }}}))const browser = await puppeteer.launch()

Reference of pref names and their access: https://chromium.googlesource.com/chromium/src/+/master/chrome/common/pref_names.cc