Puppeteer & Playwright との連携

A developer's guide with complete code examples to route Puppeteer and Playwright automated scrapers through XProxy mobile channels, including handling proxy authentication and automated IP rotation.

XProxy Console Proxy Export and Port Settings

Puppeteer integration (Node.js)

Puppeteer allows you to pass a proxy server in the launch arguments. If your XProxy requires authentication, you must use page.authenticate() before navigating.

puppeteer_example.js
const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
    headless: true,
    args: [
      // Route all traffic through XProxy SOCKS5 or HTTP port
      '--proxy-server=socks5://192.168.6.7:5001' 
    ]
  });

  const page = await browser.newPage();

  // If XProxy auth is enabled, authenticate here:
  await page.authenticate({
    username: 'your_xproxy_username',
    password: 'your_xproxy_password'
  });

  await page.goto('https://httpbin.org/ip');
  const body = await page.evaluate(() => document.body.innerText);
  console.log('Exit IP:', body);

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

Playwright integration (Node.js)

Playwright supports proxy configurations natively in the browser launch options, including the credential parameters. This avoids the need for a separate page authentication step.

playwright_example.js
const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch({
    proxy: {
      server: 'socks5://192.168.6.7:5001',
      // Optional credentials if authentication is enabled in XProxy Settings:
      username: 'your_xproxy_username',
      password: 'your_xproxy_password'
    }
  });

  const context = await browser.newContext();
  const page = await context.newPage();

  await page.goto('https://httpbin.org/ip');
  const body = await page.locator('body').innerText();
  console.log('Exit IP:', body);

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

Playwright integration (Python)

For Python developers running web automation or SEO rank checkers:

playwright_example.py
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(
        proxy={
            "server": "socks5://192.168.6.7:5001",
            # Optional credentials:
            "username": "your_xproxy_username",
            "password": "your_xproxy_password"
        }
    )
    
    page = browser.new_page()
    page.goto("https://httpbin.org/ip")
    print("Exit IP:", page.inner_text("body"))
    
    browser.close()

Triggering IP rotation in scripts

If you need a new IP address for each execution or cycle in your script, make a simple HTTP GET request to XProxy's position-based Rotation API before navigating:

Node.js Rotation Trigger
const axios = require('axios');

async function rotateIP() {
  try {
    const response = await axios.get('http://192.168.6.7:8081/api/v1/rotate_ip/position/1');
    console.log('Rotate response:', response.data);
    // Wait 5-8 seconds for 4G/5G cellular reconnection to complete
    await new Promise(resolve => setTimeout(resolve, 6000));
  } catch (error) {
    console.error('IP rotation failed:', error.message);
  }
}