Skip to main content
Version: v2

Close Browser Sessions

When finished with work, or on errors, make sure you run browser.close so that other sessions may start. browserless will terminate long-running sessions via the timeout setting, but it's always a good idea to close tidly whenever you're finished.

import puppeteer from "puppeteer-core";

const TOKEN = "YOUR_API_TOKEN_HERE";

const browser = await puppeteer.connect({
browserWSEndpoint: `wss://production-sfo.browserless.io?token=${TOKEN}`,
});

const page = await browser.newPage();

try {
await page.goto("https://www.browserless.io/");
await page.screenshot({ path: "./browserless.png" });
browser.close();
} catch (error) {
console.error({ error }, "Something happened!");
browser.close();
}

Session Timeout

By default, your sessions are governed by a timeout. This is set via your account page for the hosted service. You can override this behavior.

  • If you are running BaaS in Docker, you can specify a TIMEOUT environment variable in the docker run command to override the default timeout of 30 seconds.

  • If you need to override the timeout on a per-job basis, simply specify a timeout parameter in your connect calls query-parameters, with the value being the time in milliseconds for the session to execute:

    import puppeteer from "puppeteer-core";

    const browser = await puppeteer.connect({
    browserWSEndpoint: `wss://production-sfo.browserless.io/?token=YOUR_API_TOKEN_HERE&timeout=10000`,
    });
    // ...
note

For other runtimes and selenium libraries be sure to consult your libraries documentation, or contact us