Playwright Locator Class API
Locator is Playwright's locator object, providing chained filtering, operations, and assertions.
Locator Operation Methods
| Method | Description |
locator.click(options?) | Click element |
locator.dblclick(options?) | Double-click element |
locator.hover(options?) | Mouse hover |
locator.fill(value, options?) | Clear and fill text |
locator.type(text, options?) | Type character by character |
locator.clear(options?) | Clear input field |
locator.check(options?) | Check checkbox |
locator.uncheck(options?) | Uncheck |
locator.setChecked(checked, options?) | Set checkbox state |
locator.selectOption(values, options?) | Select dropdown option |
locator.setInputFiles(files, options?) | Set file upload |
locator.dragTo(target, options?) | Drag to target element |
locator.press(key, options?) | Press key |
locator.screenshot(options?) | Element screenshot |
locator.scrollIntoViewIfNeeded(options?) | Scroll into view |
Locator Chained Filtering
| Method | Description |
locator.first() | Get first matching element |
locator.last() | Get last matching element |
locator.nth(index) | Get the nth one (0-based) |
locator.filter(options?) | Filter by conditions (hasText, has, hasNot) |
locator.locator(selector) | Continue searching within matching results |
locator.getByRole(role, options?) | Search by role within matching results |
locator.getByText(text) | Search by text within matching results |
locator.getByLabel(text) | Search by label within matching results |
locator.and(locator) | Logical AND (1.63+) |
locator.or(locator) | Logical OR (1.63+) |
Locator Information Retrieval
| Method | Description |
locator.textContent(options?) | Get text content |
locator.innerHTML(options?) | Get innerHTML |
locator.innerText(options?) | Get visible text |
locator.inputValue(options?) | Get input value |
locator.getAttribute(name) | Get attribute value |
locator.count() | Get matching count |
locator.isVisible(options?) | Whether visible |
locator.isHidden(options?) | Whether hidden |
locator.isEnabled(options?) | Whether enabled |
locator.isDisabled(options?) | Whether disabled |
locator.isEditable(options?) | Whether editable |
locator.isChecked(options?) | Whether checked |
locator.boundingBox(options?) | Get element position and size |
Locator JS Execution
| Method | Description |
locator.evaluate(fn, arg?, options?) | Execute JS in element context |
locator.evaluateAll(fn, arg?) | Execute JS on all matching elements |
locator.evaluateHandle(fn, arg?, options?) | Execute JS and return JSHandle |
LocatorAssertions (Automatic Retry)
| Assertion Method | Description |
expect(locator).toBeAttached() | Element is in the DOM |
expect(locator).toBeChecked() | Checked |
expect(locator).toBeDisabled() | Disabled |
expect(locator).toBeEditable() | Editable |
expect(locator).toBeEmpty() | Content is empty |
expect(locator).toBeEnabled() | Enabled |
expect(locator).toBeFocused() | Has focus |
expect(locator).toBeHidden() | Element is not visible |
expect(locator).toBeInViewport() | Element is within the viewport |
expect(locator).toBeVisible() | Element is visible |
expect(locator).toContainText(text) | Contains text |
expect(locator).toHaveAccessibleDescription(desc) | Has accessible description |
expect(locator).toHaveAccessibleName(name) | Has accessible name |
expect(locator).toHaveAttribute(name, value?) | Has DOM attribute |
expect(locator).toHaveClass(className) | Has CSS class name |
expect(locator).toHaveCount(count) | Has specified count |
expect(locator).toHaveCSS(name, value) | Has CSS property value |
expect(locator).toHaveId(id) | Has ID |
expect(locator).toHaveText(text) | Text content matches |
expect(locator).toHaveValue(value) | Input value matches |
expect(locator).toHaveValues(values) | Multiple values match (multi-select) |
expect(locator).toHaveScreenshot(options?) | Element screenshot comparison |
FrameLocator Class
| Method | Description |
frameLocator.getByRole(role, options?) | Locate by role |
frameLocator.getByText(text) | Locate by text |
frameLocator.getByLabel(text) | Locate by label |
frameLocator.getByPlaceholder(text) | Locate by placeholder |
frameLocator.getByTestId(id) | Locate by testId |
frameLocator.locator(selector) | CSS/XPath locating |
frameLocator.frameLocator(selector) | Nested iframe locating |
frameLocator.first() | Get first |
frameLocator.last() | Get last |
frameLocator.nth(index) | Get the nth one |
Other extensions