A list of your recent invoices.
Sr No.Book NameBook GenreBook AuthorBook ISBNBook Published
1Gryphon. Alice did.ImpeditChhavi Din97914491289362015-02-16
2King, 'and don't.OmnisDivya Mittal97829161572211979-05-15
3So Alice began to.UndeAnand Soman97907229241041996-07-20
4Alice started to.QuiMayawati Seth97905356745931979-10-12
5And the Gryphon at.UtAnil Lall97980297244341972-05-21
6Come on!' So they.NihilGayatri Jain97800304798542002-03-18
7An obstacle that.QuiWafa Bava97957170881512010-01-04
8Duchess, digging.MinimaLata Sama97949447550891996-09-16
9Let me see: that.VoluptatibusAkshay Kata97848464069501988-07-05
10White Rabbit, 'but.TemporibusAarti Mitter97884554171781989-09-17

Insight

On completion of this exercise, you can learn the following concepts:

  1. accept()
  2. dismiss()
  3. waits()
  4. sendKeys()
  5. switchTo()
  6. getText()

Introduction

HTML tables are everywhere — dashboards, reports, transaction lists. Automating them requires:

  1. Count rows and columns — verify table size
  2. Read a specific cell — by row and column index
  3. Find a row by content — search for specific data
  4. Verify table headers — assert column names
  5. Verify all rows — loop through and check values

Key Methods Summary

Action Selenium (Java) Playwright (JS) Playwright (Python)
Count rows findElements(By.cssSelector("tbody tr")).size() locator("tbody tr").count() locator("tbody tr").count()
Count cols findElements(By.cssSelector("thead th")).size() locator("thead th").count() locator("thead th").count()
Get cell text findElement(By.cssSelector("tr:nth-child(2) td:nth-child(1)")).getText() locator("tr:nth-child(2) td:nth-child(1)").textContent() text_content()
Get headers findElements(By.tagName("th")) locator("th").allTextContents() all_text_contents()

1. Count rows and columns

Selenium (Java)

List<WebElement> rows = driver.findElements(By.cssSelector("#dataTable tbody tr"));
System.out.println("Row count: " + rows.size());
 
List<WebElement> cols = driver.findElements(By.cssSelector("#dataTable thead th"));
System.out.println("Column count: " + cols.size());

Playwright (JS)

const rowCount = await page.locator("#dataTable tbody tr").count();
const colCount = await page.locator("#dataTable thead th").count();
console.log("Rows:", rowCount, "Cols:", colCount);

Playwright (Python)

row_count = page.locator("#dataTable tbody tr").count()
col_count = page.locator("#dataTable thead th").count()
print(f"Rows: {row_count}, Cols: {col_count}")

2. Read a specific cell by row and column

Selenium (Java)

// Row 1, Column 1 (1-based in CSS)
String cellText = driver.findElement(
    By.cssSelector("#dataTable tbody tr:nth-child(1) td:nth-child(1)")
).getText();
System.out.println("Cell value: " + cellText);

Playwright (JS)

const cellText = await page
  .locator("#dataTable tbody tr:nth-child(1) td:nth-child(1)")
  .textContent();
console.log("Cell value:", cellText);

Playwright (Python)

cell_text = page.locator(
    "#dataTable tbody tr:nth-child(1) td:nth-child(1)"
).text_content()
print("Cell value:", cell_text)

3. Read all header names

Selenium (Java)

List<WebElement> headers = driver.findElements(By.cssSelector("#dataTable thead th"));
for (WebElement header : headers) {
    System.out.println(header.getText());
}

Playwright (JS)

const headers = await page.locator("#dataTable thead th").allTextContents();
console.log(headers); // ["Name", "Age", "City", "Action"]

Playwright (Python)

headers = page.locator("#dataTable thead th").all_text_contents()
print(headers)

4. Find a row containing specific text

Selenium (Java)

List<WebElement> rows = driver.findElements(By.cssSelector("#dataTable tbody tr"));
for (WebElement row : rows) {
    if (row.getText().contains("Alice")) {
        System.out.println("Found row: " + row.getText());
        // click edit button in this row
        row.findElement(By.cssSelector("button[data-action='edit']")).click();
        break;
    }
}

Playwright (JS)

const row = page.locator("#dataTable tbody tr").filter({ hasText: "Alice" });
await expect(row).toBeVisible();
await row.locator("button[data-action='edit']").click();

Playwright (Python)

row = page.locator("#dataTable tbody tr").filter(has_text="Alice")
expect(row).to_be_visible()
row.locator("button[data-action='edit']").click()

5. Read all rows and build a list

Selenium (Java)

List<WebElement> rows = driver.findElements(By.cssSelector("#dataTable tbody tr"));
List<String> names = new ArrayList<>();
for (WebElement row : rows) {
    String name = row.findElement(By.cssSelector("td:nth-child(1)")).getText();
    names.add(name);
}
System.out.println(names);

Playwright (JS)

const rows = page.locator("#dataTable tbody tr");
const count = await rows.count();
const names = [];
for (let i = 0; i < count; i++) {
  const name = await rows.nth(i).locator("td:nth-child(1)").textContent();
  names.push(name.trim());
}
console.log(names);

Playwright (Python)

rows = page.locator("#dataTable tbody tr")
count = rows.count()
names = []
for i in range(count):
    name = rows.nth(i).locator("td:nth-child(1)").text_content()
    names.append(name.strip())
print(names)

6. Verify table has no data (empty state)

Selenium (Java)

List<WebElement> rows = driver.findElements(By.cssSelector("#dataTable tbody tr"));
assertTrue(rows.isEmpty() || rows.get(0).getText().contains("No data"));

Playwright (JS)

const rowCount = await page.locator("#dataTable tbody tr").count();
expect(rowCount).toBe(0);
// or check empty message
await expect(page.locator("#emptyTableMsg")).toBeVisible();

Playwright (Python)

row_count = page.locator("#dataTable tbody tr").count()
assert row_count == 0
# or
expect(page.locator("#emptyTableMsg")).to_be_visible()

📄 Also Read: Top 10 Best Automation Practice Website