2

Set Proxy per page in a Puppeteer Browser

Using a For of loop to create a new page for each automated instance, but after both pages load and take a screenshot, whatever is the first instance to start automating first, it takes over and only that automation takes place.

setting flags from what i've seen is only doable when creating a new browser eg.

  const browser = await puppeteer.launch({args:['--proxy-server=ip:port']});

cant seem to find any docs about setting it via the page.

LunaFr0st
  • 21
  • 1
  • 4

1 Answers1

3

I made a module that does this. It's called puppeteer-page-proxy. It supports setting a proxy for an entire page, or if you like, it can set a different proxy for each request.

First install it:

npm i puppeteer-page-proxy

Then require it:

const useProxy = require('puppeteer-page-proxy');

Using it is easy; Set proxy for an entire page:

await useProxy(page, 'http://127.0.0.1:8000');

If you want a different proxy for each request,then you can simply do this:

await page.setRequestInterception(true);
page.on('request', req => {
    useProxy(req, 'socks5://127.0.0.1:9000');
});

Then if you want to be sure that your page's IP has changed, you can look it up;

const data = await useProxy.lookup(page);
console.log(data.ip);

It supports http, https, socks4 and socks5 proxies, and it also supports authentication if that is needed:

const proxy = 'http://login:pass@127.0.0.1:8000'

Repository: https://github.com/Cuadrix/puppeteer-page-proxy

Cuadrix
  • 433
  • 3
  • 10