For AI agents: a documentation index is available at /llms.txt
Skip to main content

Batch DOM Queries in a Single Browser Call

When you connect to Browserless over WebSocket, every page.$eval() call is a separate network round-trip between your Node process and a remote Chrome instance. Bundle all DOM reads into one page.evaluate() call and you pay that cost once, no matter how many selectors you need. For more performance tips with remote browsers, see BQL Best Practices.

Prerequisites

Steps

The /function endpoint runs the entire script server-side inside the same browser process, so there's naturally only one round-trip per request. No special batching needed.

View Full Code on GitHub

1. Send the request

curl -X POST \
"https://production-sfo.browserless.io/function?token=YOUR_API_TOKEN_HERE" \
-H "Content-Type: application/javascript" \
--data-raw 'module.exports = async ({ page }) => {
await page.goto("https://example.com");

const data = await page.evaluate(() => ({
title: document.title,
heading: document.querySelector("h1")?.textContent?.trim() ?? null,
links: Array.from(document.querySelectorAll("a[href]")).map(a => ({
text: a.textContent.trim(),
href: a.href,
})),
paragraphCount: document.querySelectorAll("p").length,
}));

return data;
};'

2. Check the output

{
"title": "Example Domain",
"heading": "Example Domain",
"links": [
{ "text": "More information...", "href": "https://www.iana.org/domains/reserved" }
],
"paragraphCount": 2
}