---
title: Lists, Tables and Dynamic Content | TestingBot
description: 'Most real pages are not a form with five uniquely labelled fields. They
  are lists of things: search results, todos, invoices, table rows, each with the
  same…'
source_url:
  html: https://testingbot.com/resources/courses/locator-strategies/lists-tables-and-dynamic-content
  md: https://testingbot.com/resources/courses/locator-strategies/lists-tables-and-dynamic-content.md
---

Course

[Selenium Locators and Locator Strategies](https://testingbot.com/resources/courses/locator-strategies)

Chapter 9 of 12

1. [1 Selenium Locators: Strategies That Survive Change](https://testingbot.com/resources/courses/locator-strategies)
2. [2 Why Tests Break on Locators](https://testingbot.com/resources/courses/locator-strategies/why-locators-break)
3. [3 Inspect the DOM and Test Selectors in DevTools](https://testingbot.com/resources/courses/locator-strategies/inspect-and-test-selectors-in-devtools)
4. [4 CSS Selectors for Test Automation](https://testingbot.com/resources/courses/locator-strategies/css-selectors)
5. [5 XPath in Selenium: Text, Axes and When to Use It](https://testingbot.com/resources/courses/locator-strategies/xpath-in-selenium)
6. [6 Role-Based, Label and Relative Locators](https://testingbot.com/resources/courses/locator-strategies/role-based-and-relative-locators)
7. [7 A Test ID Contract With Your Developers](https://testingbot.com/resources/courses/locator-strategies/a-test-id-contract)
8. [8 Shadow DOM and iframes](https://testingbot.com/resources/courses/locator-strategies/shadow-dom-and-iframes)
9. [9 Lists, Tables and Dynamic Content](https://testingbot.com/resources/courses/locator-strategies/lists-tables-and-dynamic-content)
10. [10 A Note on Mobile Locators](https://testingbot.com/resources/courses/locator-strategies/a-note-on-mobile-locators)
11. [11 A Locator Review Checklist](https://testingbot.com/resources/courses/locator-strategies/locator-review-checklist)
12. [12 Locator Strategies Knowledge Check](https://testingbot.com/resources/courses/locator-strategies/knowledge-check)

[Exit course](https://testingbot.com/resources/courses)

 Chapter 9 of 12 Intermediate 7 min read 2 quick checks Updated Nov 2026 

# Lists, Tables and Dynamic Content
On this page
1. [Filter, then scope](https://testingbot.com#filter-then-scope)
2. [Tables: find the row by a cell, read the cell by its column](https://testingbot.com#tables-find-the-row-by-a-cell-read-the-cell-by-its-column)
3. [Elements that appear, disappear and re-render](https://testingbot.com#elements-that-appear-disappear-and-re-render)
4. [Summary](https://testingbot.com#summary)

Most real pages are not a form with five uniquely labelled fields. They are lists of things: search results, todos, invoices, table rows, each with the same structure and the same "Edit" and "Delete" buttons. The locator problem changes from "find this element" to "find the right one of many", and the answer is almost always the same: **find the item by its content, then look inside it**.

## Filter, then scope[#](https://testingbot.com#filter-then-scope)

Playwright locators can be narrowed with `filter()` and chained, so you can say "the todo item that contains 'Walk the dog', and the checkbox inside it" (`tests/lists-and-tables.spec.ts`):

```typescript
import { test, expect } from './fixtures';

test('filter a list by text, then act inside the item', async ({ page }) => {
  await page.goto('https://demo.playwright.dev/todomvc/');
  const newTodo = page.getByPlaceholder('What needs to be done?');
  for (const title of ['Buy milk', 'Walk the dog', 'Buy bread']) {
    await newTodo.fill(title);
    await newTodo.press('Enter');
  }

  const items = page.getByTestId('todo-item');
  await expect(items).toHaveCount(3);

  await items.filter({ hasText: 'Walk the dog' }).getByRole('checkbox').check();
  await expect(items.filter({ hasText: 'Walk the dog' })).toHaveClass(/completed/);

  await expect(items.filter({ hasText: /^Buy/ })).toHaveCount(2);
  await expect(items.filter({ has: page.getByRole('checkbox', { checked: true }) })).toHaveText(['Walk the dog']);
  await expect(items.nth(2)).toHaveText('Buy bread');
});
```

- `filter({ hasText })` keeps the items whose text contains a string, or matches a regular expression.
- `filter({ has })` keeps the items that contain an element matching another locator: here, "the items whose checkbox is checked". `hasNot` and `hasNotText` do the opposite. The inner locator is matched _inside_ each item, so `has` cannot test a class on the item itself.
- `nth()`, `first()` and `last()` pick by position. Use them when the order is what you are testing (the item just added is last); otherwise filter by content.
- Asserting the whole list with `toHaveText([...])` checks content and order in one retrying assertion, which is easier to read than a loop.

A test deletes the todo "Pay rent" with `items.nth(1).getByRole('button', { name: 'Delete' }).click()`. It passes until another test starts adding a todo first. What is the robust version?

 `items.nth(2).getByRole('button', { name: 'Delete' })` `items.filter({ hasText: 'Pay rent' }).getByRole('button', { name: 'Delete' })` `page.getByRole('button', { name: 'Delete' }).first()` Run the tests in a fixed order

Filtering by the item's content finds "Pay rent" wherever it is in the list. Changing the index only moves the problem, `first()` deletes whatever happens to be first, and depending on test order creates a different kind of flaky test.

## Tables: find the row by a cell, read the cell by its column[#](https://testingbot.com#tables-find-the-row-by-a-cell-read-the-cell-by-its-column)

On `the-internet.herokuapp.com/tables`, table 1 has no ids or classes inside it at all. Role locators still work, because tables have built-in roles: `row`, `cell` and `columnheader`:

```typescript
test('find a table row by its content', async ({ page }) => {
  await page.goto('https://the-internet.herokuapp.com/tables');
  const rows = page.locator('#table1 tbody tr');
  const doe = rows.filter({ has: page.getByRole('cell', { name: 'jdoe@hotmail.com', exact: true }) });
  await expect(doe.getByRole('cell').nth(3)).toHaveText('$100.00');

  await doe.getByRole('link', { name: 'edit' }).click();
  await expect(page).toHaveURL(/#edit$/);
});

test('read a column after sorting', async ({ page }) => {
  await page.goto('https://the-internet.herokuapp.com/tables');
  const table = page.locator('#table1');
  await table.getByRole('columnheader', { name: 'Last Name' }).click();
  await expect(table.locator('tbody tr td:nth-child(1)')).toHaveText(['Bach', 'Conway', 'Doe', 'Smith']);
});
```

`exact: true` matters for cells: without it, a cell named "Doe" would also match a cell containing "Doerr". The email address identifies the row; everything else is found inside that row.

In Selenium the same idea is two small helpers, one for "the row with this cell" and one for "the cells of the column with this header". Looking the column up by its header text means a new column added to the left does not shift every index in your tests (`tests/test_tables_and_dynamic.py`):

```python
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

TABLES = "https://the-internet.herokuapp.com/tables"
DYNAMIC = "https://the-internet.herokuapp.com/dynamic_controls"

def row_with(driver, table_id, text):
    """The row of a table that has a cell with exactly this text."""
    return driver.find_element(
        By.XPATH, f"//table[@id='{table_id}']/tbody/tr[td[normalize-space()='{text}']]")

def column(driver, table_id, header):
    """All cells of the column whose header has this text."""
    headers = [th.text for th in driver.find_elements(By.CSS_SELECTOR, f"#{table_id} thead th")]
    index = headers.index(header) + 1
    return [td.text for td in driver.find_elements(
        By.CSS_SELECTOR, f"#{table_id} tbody tr > td:nth-child({index})")]

def test_scope_to_a_row(driver):
    driver.get(TABLES)
    row = row_with(driver, "table1", "fbach@yahoo.com")
    assert row.find_element(By.CSS_SELECTOR, "td:nth-child(4)").text == "$51.00"
    row.find_element(By.LINK_TEXT, "delete").click()
    assert driver.current_url.endswith("#delete")

def test_sorted_column(driver):
    driver.get(TABLES)
    driver.find_element(By.XPATH, "//table[@id='table1']//th[normalize-space()='Last Name']").click()
    assert column(driver, "table1", "Last Name") == ["Bach", "Conway", "Doe", "Smith"]
```

`row.find_element(...)` searches only inside that row, which is what makes `By.LINK_TEXT, "delete"` safe even though the table has four delete links. Scoping by calling `find_element` on an element is the Selenium equivalent of chaining locators in Playwright.

One caution for the helpers: they put text into an XPath string. A value containing a single quote (`O'Brien`) breaks the expression. Use double quotes in the XPath for such values, or match in Python over `find_elements` results.

Product adds a "Company" column as the first column of the table. Which locator for the email of the "Doe" row keeps working without changes?

 `#table1 tbody tr:nth-child(3) td:nth-child(3)` `//table[@id='table1']//tr[td[1]='Doe']/td[3]` The row found by the cell text "Doe", with the email cell's column index looked up from the "Email" header `(//td)[11]`

Every option except c hard-codes column positions, and the new first column shifts all of them by one. Looking up the row by content and the column by its header text adapts to the new layout automatically.

## Elements that appear, disappear and re-render[#](https://testingbot.com#elements-that-appear-disappear-and-re-render)

Dynamic pages add one more problem: the element you found a moment ago may no longer exist. On `the-internet.herokuapp.com/dynamic_controls`, the Remove button deletes the checkbox after a short delay and shows a message:

```python
def test_element_that_goes_away(driver):
    driver.get(DYNAMIC)
    checkbox = driver.find_element(By.CSS_SELECTOR, "#checkbox-example input[type='checkbox']")
    driver.find_element(By.XPATH, "//form[@id='checkbox-example']//button[normalize-space()='Remove']").click()
    wait = WebDriverWait(driver, 10)
    wait.until(EC.staleness_of(checkbox))
    message = wait.until(EC.visibility_of_element_located((By.ID, "message")))
    assert message.text == "It's gone!"
```

A Selenium `WebElement` is a reference to one specific node. When the page removes or re-renders that node (a React re-render replaces it with an identical-looking one), the reference goes stale and any call on it raises `StaleElementReferenceException`. Two habits avoid most of these:

- **Store locators, not elements.** Keep the `(By, value)` tuple and call `find_element` when you need the element, as late as possible. Page objects that expose methods instead of cached elements do this naturally.
- **Wait for the state you need, by locator.** `visibility_of_element_located` takes a locator and re-finds the element on every poll, so it is immune to re-renders. `staleness_of` is the one condition that deliberately takes an element: "wait until this node is gone".

Playwright's locators are re-resolved on every action and assertion, so they never go stale; that is the main reason they do not need these habits.

Dynamic pages also tempt people to wait with `time.sleep()`. A locator-based wait is both faster and more reliable; the [flaky tests course](https://testingbot.com/resources/courses/flaky-tests/timing-and-waits) covers waiting strategies in depth, and our article on [ElementClickInterceptedException](https://testingbot.com/resources/articles/selenium-elementclickinterceptedexception) covers the related case where the element exists but something is on top of it.

## Summary[#](https://testingbot.com#summary)

- Lists and tables: find the item by its content, then locate inside it.
- Playwright: `filter({ hasText })`, `filter({ has })` and chaining; `nth()` only when order is the point.
- Selenium: `row.find_element(...)` scopes a search; look up columns by header text, not index.
- Keep locators, not elements, and wait for states by locator, so re-renders do not produce stale references.

[Previous · Chapter 8 Shadow DOM and iframes](https://testingbot.com/resources/courses/locator-strategies/shadow-dom-and-iframes) [Next · Chapter 10 A Note on Mobile Locators](https://testingbot.com/resources/courses/locator-strategies/a-note-on-mobile-locators)

On this page

1. [Filter, then scope](https://testingbot.com#filter-then-scope)
2. [Tables: find the row by a cell, read the cell by its column](https://testingbot.com#tables-find-the-row-by-a-cell-read-the-cell-by-its-column)
3. [Elements that appear, disappear and re-render](https://testingbot.com#elements-that-appear-disappear-and-re-render)
4. [Summary](https://testingbot.com#summary)

Certification

Earn the free, resume-worthy TestingBot Locator Strategies Certification for this course.

[How it works](https://testingbot.com/resources/courses/locator-strategies/certification)
