Playwright Locator Api
Locator is Playwright's locator object, providing chainable filtering, operations, and assertions.
* * *
## Locator Operation Methods
| Method | Description |
| --- | --- |
| `locator.click(options?)` | Click element |
| `locator.dblclick(options?)` | Double click element |
| `locator.hover(options?)` | Hover over element |
| `locator.fill(value, options?)` | Clear and fill text |
| `locator.type(text, options?)` | Type character by character |
| `locator.clear(options?)` | Clear input box |
| `locator.check(options?)` | Check checkbox |
| `locator.uncheck(options?)` | Uncheck checkbox |
| `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?)` | Take element screenshot |
| `locator.scrollIntoViewIfNeeded(options?)` | Scroll into view |
* * *
## Locator Chain Filtering
| Method | Description |
| --- | --- |
| `locator.first()` | Get first matching element |
| `locator.last()` | Get last matching element |
| `locator.nth(index)` | Get nth element (starting from 0) |
| `locator.filter(options?)` | Filter by condition (hasText, has, hasNot) |
| `locator.locator(selector)` | Continue searching in matching results |
| `locator.getByRole(role, options?)` | Find by role in matching results |
| `locator.getByText(text)` | Find by text in matching results |
| `locator.getByLabel(text)` | Find by label in 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 match count |
| `locator.isVisible(options?)` | Is visible |
| `locator.isHidden(options?)` | Is hidden |
| `locator.isEnabled(options?)` | Is enabled |
| `locator.isDisabled(options?)` | Is disabled |
| `locator.isEditable(options?)` | Is editable |
| `locator.isChecked(options?)` | Is 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 (Auto-retry)
| Assertion Method | Description |
| --- | --- |
| `expect(locator).toBeAttached()` | Element is in DOM |
| `expect(locator).toBeChecked()` | Is checked |
| `expect(locator).toBeDisabled()` | Is disabled |
| `expect(locator).toBeEditable()` | Is editable |
| `expect(locator).toBeEmpty()` | Is empty |
| `expect(locator).toBeEnabled()` | Is enabled |
| `expect(locator).toBeFocused()` | Is focused |
| `expect(locator).toBeHidden()` | Element is hidden |
| `expect(locator).toBeInViewport()` | Element is in 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 locator |
| `frameLocator.frameLocator(selector
YouTip