Puppeteer use proxy

In puppeteer use proxy, as in Chrome, is carried out using the argument –proxy-server, which is specified when the browser starts:

args: ['--proxy-server=server:port']

where server – domain name or ip-address proxy-server, port – port.

  1. Puppeteer use proxy
  2. Proxy protocols
  3. Proxy authentication

Puppeteer use proxy

Here is the full code where we demonstrate the use of proxy in puppeteer. In this example, we connect to the proxy server 138.50.50.50:8886 and go to whoer.net to check whether puppeteer really works through the proxy:

const puppeteer = require('puppeteer');              // puppeteer

(async () => {
const browser = await puppeteer.launch({            // run browser
        args: ['--proxy-server=138.50.50.50:38886'] // use proxy
});
const page = await browser.newPage();               // open new tab
await page.goto('https://whoer.net');               // go to site
await browser.close()                               // close browser
})();

You will see a page with the data of your ip-address and some information about it (how to take a screenshot in puppeteer read in this article):

The IP address from which the connection to the resource is made will be indicated in the My IP field and it should (but not necessarily) be the same as we specified in the –proxy-server argument.

Proxy protocols

You can connect to the proxy using any of the protocols: http, https, socks4 and socks5. For http and https protocol are not required (as in the example above), then for socks4 and socks5 this must be done explicitly:

const puppeteer = require('puppeteer');

(async () => {
const browser = await puppeteer.launch({
        args: ['--proxy-server=socks5://server:port']
});
const page = await browser.newPage();

await page.goto('https://whoer.net');
await browser.close()
})();

Proxy authentication

If the proxy requires authorization, then for the http protocol it is possible to use page.authenticate(), where the login and password are indicated:

const puppeteer = require('puppeteer');

(async () => {
const browser = await puppeteer.launch({
        args: ['--proxy-server=server:port']
});
const page = await browser.newPage();
await page.authenticate({
    username: 'login',
    password: 'password',
});
await page.goto('https://whoer.net');
await browser.close()
})();

Leave a Comment