How to Filter Table Rows with Playwright’s locator.filter()
With Playwright's locator.filter(), you can take the table rows with getByRole('row') and narrow them down by their contents, such as "the row where the order number is A-12" or "the rows whose status is not shipped." In E2E tests for tables such as an admin order list or a user list, where each row has its own "Cancel" or "Delete" button, this lets you press only the button in the row you want.
Here I apply the filter() options (hasText, has, hasNot, hasNotText, visible) to a table and show the results from running them in my test environment as they were. I also checked the pitfalls that are specific to tables: a partial match on part of a cell's text can select other rows, and the header row is counted.
The test environment was Ubuntu 24.04, Node.js 22.23.5, and @playwright/test 1.63.0. I wrote the tests in TypeScript, used only Chromium (headless), and ran with one worker. The tested page is HTML loaded with page.setContent() inside the test, and I did not run a server. I did not try tables with merged cells (rowspan, colspan), or tables built from <div> elements with role="grid".
- 1. The order list table used for testing
- 2. getByRole('row') also includes the header row
- 3. Narrowing rows by text (hasText)
- 4. Because the match is partial, A-10 also matches A-100
- 5. Selecting the row whose cell value matches exactly (has)
- 6. Narrowing by the value in a specific column
- 7. The locator passed to has is searched from within the row
- 8. Be careful with the header row when excluding rows that contain some text
- 9. Excluding hidden rows (visible)
The order list table used for testing
This table has one header row in <thead> and five order rows in <tbody>. The fifth order, A-13, is hidden with style="display:none". Rows that have not shipped have a "Cancel" button. Pressing it changes the status text to "cancelled" and shows a message below the table saying that A-12 was cancelled. The page text is in Japanese, and the code examples below use the Japanese strings.
<table>
<caption>注文一覧</caption>
<thead>
<tr><th>注文番号</th><th>商品</th><th>数量</th><th>状態</th><th>操作</th></tr>
</thead>
<tbody>
<tr><td>A-10</td><td>ノート</td><td>1</td><td>発送済み</td><td></td></tr>
<tr><td>A-11</td><td>ボールペン</td><td>3</td><td>未発送</td><td><button>キャンセル</button></td></tr>
<tr><td>A-12</td><td>ノート</td><td>2</td><td>未発送</td><td><button>キャンセル</button></td></tr>
<tr><td>A-100</td><td>付箋</td><td>10</td><td>発送済み</td><td></td></tr>
<tr style="display:none"><td>A-13</td><td>消しゴム</td><td>1</td><td>未発送</td><td><button>キャンセル</button></td></tr>
</tbody>
</table>
<p id="log" role="status"></p>
When opened in a browser, the four orders other than the hidden A-13 are shown.

The test file loads this HTML every time. In the test code below, I omit this part. The file with the failing examples also sets test.use({ actionTimeout: 2000 }), which limits the time to wait for a single action to 2 seconds.
import { test, expect } from '@playwright/test';
import { readFileSync } from 'node:fs';
test.beforeEach(async ({ page }) => {
await page.setContent(readFileSync('site/orders.html', 'utf8'));
});
getByRole('row') also includes the header row
First, when I took all the table rows with getByRole('row'), the count was 5.
test('ヘッダーの行も row に入る', async ({ page }) => {
await expect(page.getByRole('row')).toHaveCount(5);
});
The HTML has six <tr> elements. The count was 5 because the header row in <thead> was included, and the hidden A-13 with display:none was not. As described in the includeHidden option of getByRole, locators that search by role do not select hidden elements by default.
Narrowing rows by text (hasText)
filter({ hasText: '未発送' }) narrows the rows to only those that contain the text meaning "not shipped". The result was two rows, A-11 and A-12, and the first cell of the first row was A-11.
test('未発送の行だけに絞る', async ({ page }) => {
const rows = page.getByRole('row').filter({ hasText: '未発送' });
await expect(rows).toHaveCount(2);
await expect(rows.getByRole('cell').first()).toHaveText('A-11');
});
hasText matches if the text appears in any cell of the row. According to the locator.filter() documentation, when you pass a string, it is a case-insensitive substring match.
You can chain filter() calls. When you chain them, only the rows that match every condition remain. Only one row, A-12, contained both "not shipped" and "notebook".
test('条件を2つ重ねる', async ({ page }) => {
const rows = page
.getByRole('row')
.filter({ hasText: '未発送' })
.filter({ hasText: 'ノート' });
await expect(rows).toHaveCount(1);
await expect(rows).toContainText('A-12');
});
Because the match is partial, A-10 also matches A-100
The fact that hasText is a partial match causes trouble when you narrow by a value like an order number, where one number is contained at the start of another. The test that checks filter({ hasText: 'A-10' }) returns one row (toHaveCount(1)) failed.
test('hasText の A-10 は A-100 にも一致する', async ({ page }) => {
const row = page.getByRole('row').filter({ hasText: 'A-10' });
await expect(row).toHaveCount(1, { timeout: 2000 });
});
Error: expect(locator).toHaveCount(expected) failed
Locator: getByRole('row').filter({ hasText: 'A-10' })
Expected: 1
Received: 2
Timeout: 2000ms
Besides the A-10 row, the A-100 row also matched, so there were two rows. As described in the Strictness section of the official documentation, actions that target a single element, such as click, throw an error when the locator matches multiple elements.
Selecting the row whose cell value matches exactly (has)
When you want an exact value match, pass a locator to has that describes an element inside the row. getByRole('cell', { name: 'A-12', exact: true }) is a cell whose name is exactly A-12. With exact: true, the comparison is case-sensitive and compares the whole string (see the exact option of getByRole).
test('A-12 の行のキャンセルボタンを押す', async ({ page }) => {
const row = page
.getByRole('row')
.filter({ has: page.getByRole('cell', { name: 'A-12', exact: true }) });
await row.getByRole('button', { name: 'キャンセル' }).click();
await expect(row).toContainText('キャンセル済み');
await expect(page.getByRole('status')).toHaveText('A-12 をキャンセルしました');
});
If you search further from the locator of the filtered row, as in row.getByRole('button', ...), only the buttons inside that row are targeted. Two Cancel buttons were visible, but only the button in the A-12 row was pressed, and its status changed to "cancelled".

has also works with elements other than cells. When I counted the rows with a button using has and the rows without one using hasNot, the results were two and three. The three rows without a button include the header row.
test('ボタンがある行とない行', async ({ page }) => {
const rows = page.getByRole('row');
await expect(rows.filter({ has: page.getByRole('button') })).toHaveCount(2);
await expect(rows.filter({ hasNot: page.getByRole('button') })).toHaveCount(3);
});
Narrowing by the value in a specific column
When you want to compare the value in a particular column, such as "the rows where the quantity is 1," hasText is not enough. filter({ hasText: '1' }) matched all four order rows, because "1" appears in each of the order numbers A-10, A-11, A-12, and A-100 (the header row has no "1", so it is not included).
To choose a column, specify it inside the locator passed to has. Here I searched for exactly the text "1" inside the CSS td:nth-child(3) (the third-column cell). The result was only the A-10 row.
test('数量が1の行', async ({ page }) => {
const rows = page.getByRole('row');
await expect(rows.filter({ hasText: '1' })).toHaveCount(4);
const qty1 = rows.filter({
has: page.locator('td:nth-child(3)').getByText('1', { exact: true }),
});
await expect(qty1).toHaveCount(1);
await expect(qty1).toContainText('A-10');
});
The locator passed to has is searched from within the row
You write the locator passed to has starting from page., but as described in the "Filter by not having child/descendant" section of the official documentation, it is searched from within the outer row, not the whole page.
As a test, when I passed a locator that started from the table (getByRole('table')) to has, it returned zero rows. This is because there is no table inside a row.
test('has に page から始まる locator を渡す', async ({ page }) => {
const row = page
.getByRole('row')
.filter({ has: page.getByRole('table').getByText('A-10', { exact: true }) });
await expect(row).toHaveCount(1, { timeout: 2000 });
});
Error: expect(locator).toHaveCount(expected) failed
Locator: getByRole('row').filter({ has: getByRole('table').getByText('A-10', { exact: true }) })
Expected: 1
Received: 0
Timeout: 2000ms
Be careful with the header row when excluding rows that contain some text
When I selected the rows that do not contain "shipped" with hasNotText, the header row was included along with A-11 and A-12, because the header row also has no "shipped" text. Below is the output of allInnerTexts().
test('発送済みでない行', async ({ page }) => {
const all = page.getByRole('row').filter({ hasNotText: '発送済み' });
console.log('row 全体:', await all.allInnerTexts());
const body = page.locator('tbody').getByRole('row').filter({ hasNotText: '発送済み' });
console.log('tbody の中:', await body.allInnerTexts());
await expect(body).toHaveCount(2);
});
row 全体: [
'注文番号\t商品\t数量\t状態\t操作',
'A-11\tボールペン\t3\t未発送\tキャンセル',
'A-12\tノート\t2\t未発送\tキャンセル'
]
tbody の中: [ 'A-11\tボールペン\t3\t未発送\tキャンセル', 'A-12\tノート\t2\t未発送\tキャンセル' ]
When I first limited the search to <tbody> with page.locator('tbody').getByRole('row') and then took the rows, the header row was excluded and two rows remained. When you use hasNot or hasNotText, or count the rows, limit the search to rows inside <tbody>, so the header row is not counted.
Excluding hidden rows (visible)
If you take rows with a CSS selector such as locator('tbody tr'), unlike getByRole, hidden rows are also selected. The rows containing "not shipped" came to three, including the hidden A-13. When I added filter({ visible: true }) on top of that, only the two visible rows remained.
test('隠れた行を外す', async ({ page }) => {
const rows = page.locator('tbody tr').filter({ hasText: '未発送' });
await expect(rows).toHaveCount(3);
await expect(rows.filter({ visible: true })).toHaveCount(2);
});
The visible option was added in Playwright 1.51 (see the locator.filter() documentation). The same documentation recommends the shorter locator.visible() if you only want to select visible elements.
The eight tests other than the two that show failures (tests/pitfall.spec.ts) are collected in tests/rows.spec.ts, and all passed when I ran them.
$ npx playwright test tests/rows.spec.ts --reporter=list
Running 8 tests using 1 worker
✓ 1 [chromium] › tests/rows.spec.ts:8:1 › ヘッダーの行も row に入る (136ms)
✓ 2 [chromium] › tests/rows.spec.ts:12:1 › 未発送の行だけに絞る (104ms)
✓ 3 [chromium] › tests/rows.spec.ts:18:1 › A-12 の行のキャンセルボタンを押す (151ms)
✓ 4 [chromium] › tests/rows.spec.ts:27:1 › 条件を2つ重ねる (118ms)
✓ 5 [chromium] › tests/rows.spec.ts:36:1 › ボタンがある行とない行 (90ms)
...(console.log の出力。上に載せたもの)
✓ 6 [chromium] › tests/rows.spec.ts:42:1 › 発送済みでない行 (124ms)
✓ 7 [chromium] › tests/rows.spec.ts:50:1 › 数量が1の行 (123ms)
✓ 8 [chromium] › tests/rows.spec.ts:60:1 › 隠れた行を外す (98ms)
8 passed (1.8s)
When you need to pin down exactly one table row, the combination of has with getByRole('cell', { name, exact: true }) is more reliable than hasText. Use the partial-match hasText for text that is unlikely to be part of another value, such as "not shipped".





