77 / 139 · 13 Browser Automation with Playwright · Advanced Locators← prev⊞ allnext →☰ Read as one page
8.2Filtering Locators
filter() narrows a locator by additional criteria — text content, child elements, or visibility.
// Find the row that contains "John Doe" and click its edit button
await page.getByRole('row')
.filter({ hasText: 'John Doe' })
.getByRole('button', { name: 'Edit' })
.click();
// Filter by child element
await page.getByRole('listitem')
.filter({ has: page.getByRole('heading', { name: 'Premium Plan' }) })
.getByRole('button', { name: 'Select' })
.click();
// Negative filter: rows that do NOT contain "Archived"
await page.getByRole('row')
.filter({ hasNotText: 'Archived' });