Using Smart Proxy Manager with Playwright

Warning

zyte-smartproxy-ca.crt should be installed in your OS for the below code to work. You can follow these instructions in order to install it.

Warning

To use headless browsers properly, you then have to use Smart Proxy Manager in “Headless Mode”, which is different than the standard mode. You need to create a Headless user through the Smart Proxy Manager user dashboard for using this user in your headless browsers. More details in Using Headless Browsers with Zyte Smart Proxy Manager.

Note

Playwright requires Node.js and npm installed and an active Smart Proxy Manager account. Log in or signup here.

Note

All the code in this documentation has been tested with Node.js v14.15.1 and playwright 1.15.0.

Option 1 - Zyte SmartProxy Playwright

Installation

Download and install Zyte SmartProxy Playwright:

$ npm install zyte-smartproxy-playwright

Sample script

Zyte SmartProxy Playwright is a client library that provides Zyte Smart Proxy Manager related functionalities over Playwright.

  1. In order to run the sample code present below save it in a file named sample.js:

const { chromium } = require('zyte-smartproxy-playwright'); // Or 'firefox' or 'webkit'

(async () => {
    const browser = await chromium.launch({
        spm_apikey: '<Smart Proxy Manager API KEY>',
        headless: false,
    });
    console.log('Before new page');
    const page = await browser.newPage({ignoreHTTPSErrors: true});

    console.log('Opening page ...');
    try {
        await page.goto('https://toscrape.com/', {timeout: 180000});
    } catch(err) {
        console.log(err);
    }

    console.log('Taking a screenshot ...');
    await page.screenshot({path: 'screenshot.png'});
    await browser.close();
})();
  1. Now run the sample code using Node:

node sample.js

To know more about what you can do with Zyte SmartProxy Playwright, check out the API Documentation.

Option 2 - Zyte SmartProxy Headless Proxy

Installation

Setup the Zyte SmartProxy (formerly Crawlera) Headless Proxy as described in Using Headless Browsers with Zyte Smart Proxy Manager.

Download and install Playwright:

$ npm install playwright

Sample script

Here is a sample script you can use to test the integration of Playwright with Smart Proxy Manager, once you have installed the Headless Proxy.

const playwright = require('playwright');

async function main() {
    var browserFirefox = await playwright.firefox.launch({
        proxy: {server: "localhost:3128"}
    });
    var url = "https://toscrape.com/"
    const page = await browserFirefox.newPage({ignoreHTTPSErrors: true});
    await page.goto(url);
    await page.screenshot({path: 'screenshot.png', fullPage: true});
    await browserFirefox.close();
};

main();