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?) | Go back |
page.goForward(options?) | Go forward |
page.reload(options?) | Reload 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 locator |
page.frameLocator(selector) | iframe locator |
Page Properties Retrieval
| Method | Description |
page.title() | Get page title |
page.url() | Get 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 JSHandle |
page.addInitScript(script) | Inject initialization script |
page.addStyleTag(options?) | Add style tag |
page.addScriptTag(options?) | Add script tag |
page.exposeFunction(name, fn) | Expose function to 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
| Event | Description |
page.on('request', handler) | Listen for request |
page.on('response', handler) | Listen for response |
page.on('dialog', handler) | Listen for popup |
page.on('download', handler) | Listen for download |
page.on('popup', handler) | Listen for popup page |
page.on('console', handler) | Listen for console |
page.on('pageerror', handler) | Listen for page error |
page.on('close', handler) | Listen for 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 front |
page.emulateMedia(options?) | Emulate media features |
page.clock | Clock object (time simulation) |
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 |
Other extensions