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

Playwright Locator Class API

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 an element
locator.dblclick(options?) Double-click an element
locator.hover(options?) Hover the mouse
locator.fill(value, options?) Clear and fill in text
locator.type(text, options?) Type character by character
locator.clear(options?) Clear the input field
locator.check(options?) Check a checkbox
locator.uncheck(options?) Uncheck
locator.setChecked(checked, options?) Set checkbox state
locator.selectOption(values, options?) Select a dropdown option
locator.setInputFiles(files, options?) Set file upload
locator.dragTo(target, options?) Drag to a target element
locator.press(key, options?) Key
locator.screenshot(options?) Element screenshot
locator.scrollIntoViewIfNeeded(options?) Scroll into view

Locator chained filtering

Method Description
locator.first() Get the first matched element
locator.last() Get the last matched element
locator.nth(index) Get the nth (starting from 0)
locator.filter(options?) Filter by condition (hasText, has, hasNot)
locator.locator(selector) Continue searching within matched results
locator.getByRole(role, options?) Find by role within matched results
locator.getByText(text) Find by text within matched results
locator.getByLabel(text) Find by tag within matched 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?) 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 the element context
locator.evaluateAll(fn, arg?) Execute JS on all matched elements
locator.evaluateHandle(fn, arg?, options?) Execute JS and return a JSHandle

LocatorAssertions (auto-retrying)

Assertion methods Description
expect(locator).toBeAttached() Element is in the 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 not visible
expect(locator).toBeInViewport() Element is in 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
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 tag
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 the first
frameLocator.last() Get the last
frameLocator.nth(index) Get the nth