Playwright Core Classes API

This appendix lists the common APIs of Playwright core classes for quick reference.


Playwright Class

Playwright is the entry object of the Playwright Library.

Methods/PropertiesDescription
playwright.chromiumReturns the BrowserType object for Chromium
playwright.firefoxReturns the BrowserType object for Firefox
playwright.webkitReturns the BrowserType object for WebKit
playwright.requestReturns the APIRequest object
playwright.selectorsReturns the Selectors object

BrowserType Class

BrowserType is used to launch and connect to browsers.

MethodsDescription
browserType.launch(options?)Launches a browser instance
browserType.launchPersistentContext(userDataDir, options?)Launches a persistent context (using a local user data directory)
browserType.connect(url, options?)Connects to an already running browser
browserType.connectOverCDP(url, options?)Connects 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.

Methods/PropertiesDescription
browser.newContext(options?)Creates a new BrowserContext
browser.newPage(options?)Creates a new Page in a new Context
browser.contexts()Returns a list of all Contexts
browser.close()Closes the browser and all pages
browser.browserType()Returns the corresponding BrowserType
browser.version()Returns the browser version
browser.isConnected()Whether connected

BrowserContext Class

BrowserContext represents an independent browser context.

MethodsDescription
context.newPage()Creates a new page
context.pages()Returns all open pages
context.close()Closes the Context
context.cookies(urls?)Gets cookies
context.addCookies(cookies)Adds cookies
context.clearCookies()Clears all cookies
context.storageState(options?)Gets the current storage state (for authentication reuse)
context.grantPermissions(permissions)Grants permissions
context.clearPermissions()Clears permissions
context.setGeolocation(geolocation)Sets geolocation
context.setExtraHTTPHeaders(headers)Sets extra HTTP headers
context.route(url, handler)Intercepts network requests
context.unroute(url, handler?)Cancels interception
context.exposeFunction(name, fn)Exposes a function to the page
context.exposeBinding(name, callback)Exposes a binding to the page

BrowserServer Class

BrowserServer represents the browser process launched vialaunchServer()the launched browser process.

MethodsDescription
browserServer.wsEndpoint()Returns the WebSocket endpoint URL (for remote connection)
browserServer.close()Closes the server and browser
browserServer.kill()Forcefully terminates the browser process
Other extensions