Is there a way to use a proxy in Puppeteer for Firefox?

Viewed 2970

Is there a way to configure Puppeteer to use a proxy with Firefox, without manually having to adjust my operating system's proxy settings?

I am able to accomplish this in Chrome by using the command line argument args: [ '--proxy-server=http://0.0.0.0:0000' ], but Firefox doesn't seem to have this capability.

3 Answers

Unfortunately, there is no 'proxy-server' argument in Firefox.

However, you can intercept the request and set a proxy with the puppeteer-proxy library.

Here is an example.

import puppeteer from 'puppeteer';
import { proxyRequest } from 'puppeteer-proxy';

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  await page.setRequestInterception(true);

  page.on('request', async (request) => {
    await proxyRequest({
      page,
      proxyUrl: 'http://127.0.0.1:3000',
      request,
    });
  });

  await page.goto('http://gajus.com');
})();

It will work in Chrome and Firefox as well.

With Yevhen's example, you may run into issue's using the import statement. Instead I recommend using the following:

const puppeteer = require('puppeteer');
const { proxyRequest } = require('puppeteer-proxy');

Proxies in Firefox can be configured via preferences. Here a list of these with their default values:

pref("network.proxy.ftp",                   "");
pref("network.proxy.ftp_port",              0);
pref("network.proxy.http",                  "");
pref("network.proxy.http_port",             0);
pref("network.proxy.ssl",                   "");
pref("network.proxy.ssl_port",              0);
pref("network.proxy.socks",                 "");
pref("network.proxy.socks_port",            0);
pref("network.proxy.socks_version",         5);
pref("network.proxy.proxy_over_tls",        true);
pref("network.proxy.no_proxies_on",         "");

To actually make use of them install the official puppeteer node.js package with Firefox as selected product (note that puppeteer-firefox is deprecated). Then preferences can be specified via extraPrefsFirefox for the call to puppeteer.launch(). Here an example for the necessary steps from the puppeteer repository.

Related