2

I am using Playwright for the e2e tests. The problem statement that I have is that I want to get the text contents of all the rows of a specific column of a table. The table in question is just like the ones displayed here - https://ant.design/components/table/

A similar HTML -> HTML of table

(although each td inside the tr has a different class in my case).

Let's just say if I want to get all the age from the Age column in the table.

I've tried a couple of approaches but all of them end up with Promise rejected or Promise waiting and the test times out.

Eg -

let table_row = page.locator("tbody.ant-table-tbody tr");   // get the tr tags
       let text = table_row.locator('td:nth-child(2)');
       const arrayoftext = await page.$$eval(text, links=>
           links.map(link.textContent) )

I've asked a similar question related to Puppeteer here but I am trying to approach this in a more Playwright-y way using page.locator if possible.

demouser123
  • 4,108
  • 9
  • 50
  • 82

2 Answers2

2
const textsFromThirdColumn = [];    
const rowCount = await page.locator('.ant-table-tbody').locator('tr').count();

for (let i = 0; i < rowCount; i++) {
textsFromThirdColumn.push(await page.locator('.ant-table-tbody').locator('tr').nth(i).locator('td').nth(2).innerText())
}
Vlad Kras
  • 41
  • 3
  • Sorry I may have incorrectly explained the question. I want to get the text from all rows of a specific column. This would work fine for a single column, single row. How do I get this for all rows? – demouser123 Jun 08 '22 at 04:02
  • You can just use a for loop. Updated my answer – Vlad Kras Jun 08 '22 at 13:12
2

With Playwright 1.21 there is a new method was introduced called the :scope. You can read more about it here. Using this your can optimize your code like this:

const row = page.locator('tbody.ant-table-tbody tr[data-row-key="3"]')
const rowTexts = await row.locator(':scope').allInnerTexts()
await rowTexts.forEach((text) => {
  console.log(text)
})
Alapan Das
  • 17,144
  • 3
  • 29
  • 52
  • Marked this as expected answer. Although can we use map/filter in the same to achieve the result? – demouser123 Jun 10 '22 at 03:44
  • Yes, you can, but if I understood correctly from your question, you were looking for something that solves the problem using playwright commands. This is the closest I could get. – Alapan Das Jun 10 '22 at 04:16
  • 1
    Link to :scope is dead, also I don't see how it helps say getting all values of column 2 of the table – Eric Burel Feb 02 '23 at 13:05