The ZenRows® Scraping Browser is a robust, managed scraping solution that simplifies the web scraping process by leveraging our extensive infrastructure. When integrated with Playwright, it enables you to harness the power of our rotating residential IP pool of over 55 million IPs from 190+ countries, all while enjoying a 99.9% uptime.

In this guide, we’ll show you how to seamlessly integrate ZenRows Scraping Browser into your Playwright scripts with just one line of code.

Installing the required libraries

To get started with Playwright, you need to install the core Playwright library. You can install it by running the following command:

npm install playwright

If you want to use the ZenRows Browser SDK, install it as well:

npm install @zenrows/browser-sdk

Quick Integration with ZenRows Scraping Browser

Without the SDK

If you already have a Playwright script set up, integrating with ZenRows Scraping Browser is straightforward. For instance, if your Playwright code looks like this:

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

(async () => {
    const browser = await chromium.launch();
    const page = await browser.newPage();
    await page.goto('https://example.com');
    console.log(await page.title());
    await browser.close();
})();

You can easily switch to using ZenRows Scraping Browser by modifying just one line. Replace the launch() method with the connection URL for the ZenRows Scraping Browser. Here’s the updated code:

scraper.js
const { chromium } = require('playwright');
const connectionURL = 'wss://browser.zenrows.com?apikey=YOUR_ZENROWS_API_KEY';

(async () => {
    const browser = await chromium.connectOverCDP(connectionURL);
    const page = await browser.newPage();
    await page.goto('https://example.com');
    console.log(await page.title());
    await browser.close();
})();

With this simple change, you leverage ZenRows Scraping Browser’s advanced capabilities, including robust IP rotation and extensive global coverage.

With the SDK

Using the ZenRows Browser SDK simplifies the process further, allowing you to manage API keys and connection URLs more efficiently:

scraper.js
const { chromium } = require('playwright');
const { ScrapingBrowser } = require('@zenrows/browser-sdk');
const scrapingBrowser = new ScrapingBrowser({ apiKey: 'YOUR_ZENROWS_API_KEY'})

(async () => {
    const connectionURL = scrapingBrowser.getConnectURL();
    const browser = await chromium.connectOverCDP(connectionURL);
    const page = await browser.newPage();
    await page.goto('https://example.com');
    console.log(await page.title());
    await browser.close();
})();

The SDK handles API key management and makes the connection seamless with fewer manual steps.

Practical Use Cases

Here are some common use cases when using ZenRows Scraping Browser with Playwright.

Navigate to a webpage, extract the content, and scrape data:

const page = browser.newPage();
console.log('Navigating...');
await page.goto("https://www.example.com");
console.log(await page.title());
console.log('Scraping page content...');
const html = await page.content();
console.log(html);
await browser.close();

Taking a Screenshot

Capture screenshots during navigation:

const page = await browser.newPage();
console.log('Navigating...');
await page.goto('https://www.example.com');
console.log(await page.title());
console.log('Taking screenshot...');
await page.screenshot({ path: 'example.png' });
console.log('Screenshot saved as example.png');
await browser.close();

Running Custom Code

Execute custom JavaScript code in the browser:

const page = await browser.newPage();
console.log('Navigating...');
await page.goto('https://www.example.com');

const result = await page.evaluate(() => {
    return document.title;
});

console.log('Page title:', result);

await browser.close();

Troubleshooting

Here are some common issues you might encounter when using the Scraping Browser:

Connection Refused

If you receive a Connection Refused error, it might be due to:

  • API Key Issues: Verify that you’re using the correct API key.
  • Network Issues: Check your internet connection and firewall settings.
  • WebSocket Endpoint: Ensure that the WebSocket URL (wss://browser.zenrows.com) is correct.

Timeout Errors

If Playwright times out while trying to load a page, consider:

  • Slow Websites: Increase the timeout value by passing an option:
scraper.js
await page.goto('https://example.com', { timeout: 60000 });  // 60 seconds
  • Blocked IPs: Although ZenRows rotates IPs, some websites may still block them. Try adjusting the region or country settings.

Frequently Asked Questions (FAQ)