Skip to main content

Best Practices

There are several ways providing your efficient sessions other than a setup and a launch. Go over some main points concerning properly functioning headless scripts:

Close your session

Facing errors or when finishing the session ensure that browser.close is launched to start any other sessions. Certainly, long-running sessions are completed by BrowserCloud using a timeout setting. However, finishing your work, you should better just appropriately close it.

const browser = await puppeteer.connect({
browserWSEndpoint: `wss://`,

const page = await browser.newPage();

try {
await page.goto('');
await page.screenshot({ path: './browsercloud.png' });
} catch (error) {
console.error({ error }, 'Something happened!');

Aviod await's

Note that your command with await in front of it (or .then's) goes back and forth from the app to BrowserCloud as most of the puppeteer is async. Use various ways to limit it like using page.evaluate instead of page.$selector as in one evaluate much can be performed compared to multiple $selector calls.


const $button = await page.$('.buy-now');
const buttonText = await $button.getProperty('innerText');
const clicked = await $;


const buttonText = await page.evaluate(() => {
const $button = document.querySelector('.buy-now');
const clicked = $;

return $button.innerText;