Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Playwright Core Class APIs

Playwright Core Class APIs

This appendix lists commonly used APIs of Playwright's core classes for quick reference.


Playwright class

Playwright is the entry object of the Playwright Library.

Method/attribute Description
playwright.chromium Returns the BrowserType object for Chromium
playwright.firefox Returns the BrowserType object for Firefox
playwright.webkit Returns the BrowserType object for WebKit
playwright.request Returns the APIRequest object
playwright.selectors Returns the Selectors object

BrowserType class

BrowserType is used to launch and connect to browsers.

Method Description
browserType.launch(options?) Launch a browser instance
browserType.launchPersistentContext(userDataDir, options?) Launch a persistent Context (using a local user data directory)
browserType.connect(url, options?) Connect to an already-running browser
browserType.connectOverCDP(url, options?) Connect to an already-running browser via the CDP protocol
browserType.executablePath() Returns the browser executable path
browserType.name() Returns the browser name

Browser class

Browser represents a browser instance.

Method/attribute Description
browser.newContext(options?) Create a new BrowserContext
browser.newPage(options?) Create a new Page in a new Context
browser.contexts() Returns a list of all Contexts
browser.close() Close the browser and all pages
browser.browserType() Returns the corresponding BrowserType
browser.version() Returns the browser version
browser.isConnected() Whether it is connected

BrowserContext class

BrowserContext represents an independent browser context.

Method Description
context.newPage() Create a new page
context.pages() Returns all open pages
context.close() Close the Context
context.cookies(urls?) Get a cookie
context.addCookies(cookies) Add a cookie
context.clearCookies() Clear all cookies
context.storageState(options?) Get the current storage state (for authentication reuse)
context.grantPermissions(permissions) Grant permissions
context.clearPermissions() Clear permissions
context.setGeolocation(geolocation) Set geolocation
context.setExtraHTTPHeaders(headers) Set additional HTTP headers
context.route(url, handler) Intercept network requests
context.unroute(url, handler?) Cancel interception
context.exposeFunction(name, fn) Expose a function to the page
context.exposeBinding(name, callback) Expose a binding to the page

BrowserServer class

BrowserServer represents the browser process launched via launchServer() the launch server.

Method Description
browserServer.wsEndpoint() Returns the WebSocket endpoint URL (for remote connections)
browserServer.close() Close the server and browser
browserServer.kill() Forcefully terminate the browser process