BrowserType.connect method
async method that attaches Playwright to an existing browser instance created via BrowserType.launchServer. Returns a Browser. Available since v1.8. The major and minor version of the Playwright instance that connects needs to match the version of Playwright that launches the browser (1.2.3 → is compatible with 1.2.x).
BrowserType.connect parameters and options
BrowserType.connect accepts the following:
Parameter:
- endpoint (string, since v1.10): A Playwright browser websocket endpoint to connect to. You obtain this endpoint via BrowserServer.wsEndpoint.
Options:
- headers (Object<string, string>, since v1.11): Additional HTTP headers to be sent with web socket connect request. Optional.
- slowMo (float, since v1.10): Slows down Playwright operations by the specified amount of milliseconds. Defaults to 0.
- timeout (float, since v1.10): Maximum time in milliseconds to wait for the connection to be established. Defaults to 0 (no timeout).
- exposeNetwork (string, since v1.37): Exposes network available on the connecting client to the browser being connected to. Consists of a list of rules separated by comma. Available rules: hostname pattern (e.g., example.com, *.org:99, x.*.y.com, *foo.org), IP literal (e.g., 127.0.0.1, 0.0.0.0:99, [::1], [0:0::1]:99), or <loopback> that matches local loopback interfaces.
BrowserType.connectOverCDP method
async method that attaches Playwright to an existing browser instance using the Chrome DevTools Protocol. Returns a Browser. Available since v1.9. The default browser context is accessible via Browser.contexts. Connecting over the Chrome DevTools Protocol is only supported for Chromium-based browsers. This connection is significantly lower fidelity than the Playwright protocol connection via BrowserType.connect.
BrowserType.connectOverCDP parameters and options
BrowserType.connectOverCDP accepts the following:
Parameter:
- endpointURL (string, since v1.11): A CDP websocket endpoint or http url to connect to. For example http://localhost:9222/ or ws://127.0.0.1:9222/devtools/browser/387adf4c-243f-4051-a181-46798f4a46f4.
Options:
- endpointURL (string, since v1.14, JavaScript only, deprecated): Use the first argument instead.
- headers (Object<string, string>, since v1.11): Additional HTTP headers to be sent with connect request. Optional.
- isLocal (boolean, since v1.58): Tells Playwright that it runs on the same host as the CDP server. Enables certain optimizations that rely upon the file system being the same between Playwright and the Browser.
- slowMo (float, since v1.11): Slows down Playwright operations by the specified amount of milliseconds. Defaults to 0.
- timeout (float, since v1.11): Maximum time in milliseconds to wait for the connection to be established. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.
- noDefaults (boolean, since v1.60): When true, Playwright will not apply its default overrides to the existing default browser context. Defaults to false.
- artifactsDir (path, since v1.61): If specified, browser artifacts (such as traces and downloads) are saved into this directory.
BrowserType.connectOverCDP example
const browser = await playwright.chromium.connectOverCDP('http://localhost:9222');
const defaultContext = browser.contexts()[0];
const page = defaultContext.pages()[0];
BrowserType.executablePath method
Returns a string representing a path where Playwright expects to find a bundled browser executable. Available since v1.8.
BrowserType.launch method
async method that launches and returns a browser instance. Available since v1.8. The method accepts various options including ignoreDefaultArgs to filter out specific command-line arguments from defaults.
BrowserType.launch example
const browser = await chromium.launch({ // Or 'firefox' or 'webkit'.
ignoreDefaultArgs: ['--mute-audio']
});
BrowserType.launchPersistentContext method
async method that launches a browser using persistent storage and returns the only browser context instance. Closing this context will automatically close the browser. Available since v1.8. Returns a BrowserContext.
BrowserType.launchPersistentContext userDataDir parameter
BrowserType.launchPersistentContext requires a userDataDir parameter (path, since v1.8) that specifies the path to a User Data Directory, which stores browser session data like cookies and local storage. Pass an empty string to create a temporary directory. Browsers do not allow launching multiple instances with the same User Data Directory. For Chromium, this is the parent directory of the "Profile Path" seen at chrome://version.
BrowserType.launchServer method
async method available in JavaScript (since v1.8) that launches a browser server that a client can connect to later via BrowserType.connect. Returns a BrowserServer. The major/minor client/server version must match (1.2.3 → is compatible with 1.2.x).
BrowserType.launchServer options: host, port, wsPath
BrowserType.launchServer accepts the following options:
- host (string, since v1.45): Host to use for the web socket. Defaults to localhost, accepting connections only from the loopback interface. Pass an explicit address (e.g., 0.0.0.0) to accept connections from the network.
- port (int, since v1.8): Port to use for the web socket. Defaults to 0 that picks any available port.
- wsPath (string, since v1.15): Path at which to serve the Browser Server. For security, this defaults to an unguessable string.
BrowserType.launchServer example
const { chromium } = require('playwright'); // Or 'webkit' or 'firefox'.
(async () => {
const browserServer = await chromium.launchServer();
const wsEndpoint = browserServer.wsEndpoint();
// Use web socket endpoint later to establish a connection.
const browser = await chromium.connect(wsEndpoint);
// Close browser instance.
await browserServer.close();
})();
BrowserType.name method
Returns a string representing the browser name. For example: 'chromium', 'webkit' or 'firefox'. Available since v1.8.