Browser
A Browser is created via browserType.launch(). An example of using a Browser to create a Page:
const { firefox } = require('playwright'); // Or 'chromium' or 'webkit'.
(async () => {
const browser = await firefox.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await browser.close();
})();
Methods
browserType
Added in: v1.23Get the browser type (chromium, firefox or webkit) that the browser belongs to.
Usage
browser.browserType();
Returns
close
Added before v1.9In case this browser is obtained using browserType.launch(), closes the browser and all of its pages (if any were opened).
In case this browser is connected to, clears all created contexts belonging to this browser and disconnects from the browser server.
This is similar to force-quitting the browser. To close pages gracefully and ensure you receive page close events, call browserContext.close() on any BrowserContext instances you explicitly created earlier using browser.newContext() before calling browser.close().
The Browser object itself is considered to be disposed and cannot be used anymore.
Usage
await browser.close();
await browser.close(options);
Arguments
options
Object (optional)
Returns
contexts
Added before v1.9Returns an array of all open browser contexts. In a newly created browser, this will return zero browser contexts.
Usage
const browser = await pw.webkit.launch();
console.log(browser.contexts().length); // prints `0`
const context = await browser.newContext();
console.log(browser.contexts().length); // prints `1`
Returns
isConnected
Added before v1.9Indicates that the browser is connected.
Usage
browser.isConnected();
Returns
newBrowserCDPSession
Added in: v1.11CDP Sessions are only supported on Chromium-based browsers.
Returns the newly created browser session.
Usage
await browser.newBrowserCDPSession();
Returns
newContext
Added before v1.9Creates a new browser context. It won't share cookies/cache with other browser contexts.
If directly using this method to create BrowserContexts, it is best practice to explicitly close the returned context via browserContext.close() when your code is done with the BrowserContext, and before calling browser.close(). This will ensure the context
is closed gracefully and any artifacts—like HARs and videos—are fully flushed and saved.
Usage
(async () => {
const browser = await playwright.firefox.launch(); // Or 'chromium' or 'webkit'.
// Create a new incognito browser context.
const context = await browser.newContext();
// Create a new page in a pristine context.
const page = await context.newPage();
await page.goto('https://example.com');
// Gracefully close up everything
await context.close();
await browser.close();
})();
Arguments
options
Object (optional)-
acceptDownloads
boolean (optional)#Whether to automatically download all the attachments. Defaults to
true
where all the downloads are accepted. -
When using page.goto(), page.route(), page.waitForURL(), page.waitForRequest(), or page.waitForResponse() it takes the base URL in consideration by using the
URL()
constructor for building the corresponding URL. Unset by default. Examples:- baseURL:
http://localhost:3000
and navigating to/bar.html
results inhttp://localhost:3000/bar.html
- baseURL:
http://localhost:3000/foo/
and navigating to./bar.html
results inhttp://localhost:3000/foo/bar.html
- baseURL:
http://localhost:3000/foo
(without trailing slash) and navigating to./bar.html
results inhttp://localhost:3000/bar.html
- baseURL:
-
Toggles bypassing page's Content-Security-Policy. Defaults to
false
. -
clientCertificates
Array<Object> (optional) Added in: 1.46#-
origin
stringExact origin that the certificate is valid for. Origin includes
https
-
-