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

Playwright Page Class API Reference

Playwright Page Class API Reference

Page is the most commonly used class in Playwright, providing all methods for page interaction.


Navigation methods

Method Description
page.goto(url, options?) Navigate to URL
page.goBack(options?) Backspace
page.goForward(options?) Go forward
page.reload(options?) Refresh the page

Locator methods

Method Description
page.getByRole(role, options?) Locate by ARIA role
page.getByText(text, options?) Locate by text content
page.getByLabel(text, options?) Locate by associated label
page.getByPlaceholder(text) Locate by placeholder
page.getByAltText(text) Locate by image alt attribute
page.getByTitle(text) Locate by title attribute
page.getByTestId(id) Locate by data-testid
page.locator(selector) CSS / XPath locating
page.frameLocator(selector) iframe locating

Get page properties

Method Description
page.title() Get the page title
page.url() Get the current URL
page.content() Get full HTML content
page.viewportSize() Get viewport size

Screenshots and PDF

Method Description
page.screenshot(options?) Page screenshot
page.pdf(options?) Generate PDF (Chromium only)
page.video() Get video object

Keyboard and mouse

property Description
page.keyboard Keyboard object
page.mouse Mouse object
page.touchscreen Touchscreen object

JS execution

Method Description
page.evaluate(fn, arg?) Execute JS in page context
page.evaluateHandle(fn, arg?) Execute JS and return a JSHandle
page.addInitScript(script) Inject initialization script
page.addStyleTag(options?) Add style tag
page.addScriptTag(options?) Add script tag
page.exposeFunction(name, fn) Expose a function to the page

Waiting methods

Method Description
page.waitForURL(url, options?) Wait for URL change
page.waitForLoadState(state?) Wait for load state
page.waitForTimeout(ms) Fixed wait
page.waitForEvent(event, options?) Wait for event
page.waitForResponse(urlOrPredicate, options?) Wait for network response
page.waitForRequest(urlOrPredicate, options?) Wait for network request

Network and routing

Method Description
page.route(url, handler) Intercept network requests
page.unroute(url, handler?) Cancel interception
page.routeFromHAR(har, options?) Replay network from HAR file

Events

Events Description
page.on('request', handler) Listen to request
page.on('response', handler) Listen to response
page.on('dialog', handler) Listen to dialogs
page.on('download', handler) Listen to downloads
page.on('popup', handler) Listen to popup pages
page.on('console', handler) Listen to console
page.on('pageerror', handler) Listen to page errors
page.on('close', handler) Listen to page close

Frame related

Method Description
page.mainFrame() Get main frame
page.frames() Get all Frames

Others

Method Description
page.context() Get owning BrowserContext
page.close() Close page
page.pause() Pause execution (for debugging)
page.setViewportSize(size) Set viewport size
page.bringToFront() Bring page to foreground
page.emulateMedia(options?) Emulate media features
page.clock Clock object (time emulation)

PageAssertions (auto-retrying assertions)

Assertion methods Description
expect(page).toHaveTitle(title) Page title assertion
expect(page).toHaveURL(url) Page URL assertion
expect(page).toHaveScreenshot(options?) Page screenshot comparison