How to use proxy per page with Puppeteer

Posted January 20, 2022 by Dmytro Krasun ‐ 1 min read

t is easy to use proxy globally for the puppeteer instance, but there is a trick to use proxy on a per-page basis.

Puppeteer allows you to automate everything you can do in the browser manually and even more. There are cases when you need to use a proxy. It is easy to use proxy globally for the puppeteer instance, but there is a trick to use proxy on a per-page basis.

Make sure you installed the puppeteer library first:

npm i puppeteer

We are going to use an excellent plugin to solve the problem with proxy per page:

npm i puppeteer-page-proxy

Then you can import the library and use proxy on per-page basis:

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

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

       try {
           const page = await browser.newPage();

           useProxy(page, '127.0.0.1:9876')

           await page.goto('https://example.com/', {
               waitUntil: 'networkidle0',
           });

           await page.screenshot({ path: 'example.com.png' })
       } catch (e) {
           console.log(e)
       } finally {
           await browser.close();
       }
})();

That’s it. If you want to check out the complete guide on how to take screenshots with Puppeteer, you are welcome, and have a nice day!