It is easy to use proxy globally for the puppeteer instance, but there is a trick to use proxy on a per-page basis.
How to use proxy per page with Puppeteer
It 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 puppeteer-page-proxy to solve the problem with proxy per page.
The plugin supports:
http
, https
, socks4
and socks5
proxies;Install:
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, "socks5://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(); }})();
In this example I take a screenshot through proxy.
To use authentication with proxying, you need to specify user and password in your proxy connection URL:
const puppeteer = require("puppeteer");const useProxy = require("puppeteer-page-proxy");
(async () => { const browser = await puppeteer.launch({});
try { const page = await browser.newPage();
useProxy(page, "https://user:password@host:port");
await page.goto("https://example.com/", { waitUntil: "networkidle0", });
await page.screenshot({ path: "example.com.png" }); } catch (e) { console.log(e); } finally { await browser.close(); }})();
You can go further and proxy on per request basis:
const puppeteer = require("puppeteer");const useProxy = require("puppeteer-page-proxy");
(async () => { const browser = await puppeteer.launch({});
try { const page = await browser.newPage();
await page.setRequestInterception(true); page.on("request", async (request) => { await useProxy(request, "https://127.0.0.1:443"); });
await page.goto("https://example.com/", { waitUntil: "networkidle0", });
await page.screenshot({ path: "example.com.png" }); } catch (e) { console.log(e); } finally { await browser.close(); }})();
This example is not different from the regular usage because we proxy every page request. But! Imagine you don’t want to load your proxy bandwidth and skip proxying images. This way, it becomes more valuable:
const puppeteer = require("puppeteer");const useProxy = require("puppeteer-page-proxy");
(async () => { const browser = await puppeteer.launch({});
try { const page = await browser.newPage();
await page.setRequestInterception(true); page.on("request", async (request) => { if (request.resourceType() === "image") { request.abort(); } else { await useProxy(request, "socks4://127.0.0.1:1080"); } });
await page.goto("https://example.com/", { waitUntil: "networkidle0", });
await page.screenshot({ path: "example.com.png" }); } catch (e) { console.log(e); } finally { await browser.close(); }})();
The plugin covers as many cases as you can image.
If, in your case, you want to take screenshots or render HTML and you are OK with saving time and money, you can use our screenshot API with proxy to take screenshots.
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!
Interviews, tips, guides, industry best practices, and news.
Let's talk about the captureBeyondViewport parameter introduced in Chrome DevTools Protocol which will soon be supported by Puppeteer or even supported now at the time when you are reading the post.
In this article, I share how to fix the "execution context was destroyed, most likely because of a navigation" error that might happen while using Puppeteer.
Join me in exploring how to find the ideal wait time or event of when to take the page screenshot with Puppeteer.
Exhaustive documentation, ready SDKs, no-code tools, and other automation to help you render website screenshots and outsource all the boring work related to that to us.