Skip to content

Puppeteer Integration

Puppeteer is a Node.js library that provides a high-level API to control Chrome/Chromium.

Installation

bash
npm install puppeteer
# or
yarn add puppeteer

Basic Configuration

With Proxy

javascript
const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
    args: [
      '--proxy-server=proxy.okkproxy.com:8080'
    ]
  });

  const page = await browser.newPage();

  // Authenticate proxy
  await page.authenticate({
    username: 'your_username',
    password: 'your_password'
  });

  await page.goto('https://example.com');

  // Check IP
  await page.goto('https://ipinfo.io');
  const content = await page.content();
  console.log(content);

  await browser.close();
})();

Advanced Examples

Multiple Pages with Same Proxy

javascript
const browser = await puppeteer.launch({
  args: ['--proxy-server=proxy.okkproxy.com:8080']
});

// Page 1
const page1 = await browser.newPage();
await page1.authenticate({username: 'user', password: 'pass'});
await page1.goto('https://example1.com');

// Page 2
const page2 = await browser.newPage();
await page2.authenticate({username: 'user', password: 'pass'});
await page2.goto('https://example2.com');

await browser.close();

Screenshot with Proxy

javascript
const browser = await puppeteer.launch({
  args: ['--proxy-server=proxy.okkproxy.com:8080']
});

const page = await browser.newPage();
await page.authenticate({
  username: 'your_username',
  password: 'your_password'
});

await page.goto('https://example.com');
await page.screenshot({ path: 'screenshot.png' });

await browser.close();

Handle Proxy Errors

javascript
const browser = await puppeteer.launch({
  args: ['--proxy-server=proxy.okkproxy.com:8080']
});

const page = await browser.newPage();

try {
  await page.authenticate({
    username: 'your_username',
    password: 'your_password'
  });

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

  console.log('Success!');
} catch (error) {
  console.error('Proxy error:', error);
} finally {
  await browser.close();
}

Best Practices

1. Headless Mode

javascript
const browser = await puppeteer.launch({
  headless: true,  // or 'new' for new headless mode
  args: ['--proxy-server=proxy.okkproxy.com:8080']
});

2. Custom User Agent

javascript
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 ...');

3. Block Resources

javascript
await page.setRequestInterception(true);
page.on('request', (req) => {
  if(['image', 'stylesheet', 'font'].includes(req.resourceType())){
    req.abort();
  } else {
    req.continue();
  }
});

4. Handle Timeouts

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

Common Issues

Proxy Authentication Failed

  • Check username and password
  • Verify account has sufficient balance

Connection Timeout

  • Increase timeout value
  • Try different proxy node

Memory Leak

  • Always close browser properly
  • Use browser.close() in finally block

Resources