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.
- Puppeteer
- Playwright
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();
}
import playwright from "playwright";
const TOKEN = "YOUR_API_TOKEN_HERE";
const browser = await playwright.chromium.connectOverCDP(
`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 aTIMEOUT
environment variable in thedocker 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:- Puppeteer
- Playwright
import puppeteer from "puppeteer-core";
const browser = await puppeteer.connect({
browserWSEndpoint: `wss://production-sfo.browserless.io/?token=YOUR_API_TOKEN_HERE&timeout=10000`,
});
// ...import playwright from "playwright";
const browser = await playwright.chromium.connectOverCDP(
`wss://production-sfo.browserless.io/?token=YOUR_API_TOKEN_HERE&timeout=10000`
);
timeout
: Maximum session duration (in milliseconds) before automatic termination. Default: 30 seconds (30000ms). Available across all APIs.ttl
: Time to Live - browser persistence duration after disconnection for reconnection scenarios (in milliseconds). Available across all APIs.
For other runtimes and selenium libraries be sure to consult your libraries documentation, or contact us