如何从剧作家中的表格中获取特定列的文本

发布于 2025-02-05 18:18:45 字数 1068 浏览 2 评论 0原文

我正在使用剧作家进行E2E测试。我拥有的问题陈述是我想获取表特定列的所有行的文本内容。所讨论的表就像在此处显示的表一样 - https:// https://ant.design/components/components/table/

类似的html - >

(尽管tr内的每个td在我的情况下都有不同的类)。

我们只想说我是否想从表格中的年龄列中获得所有年龄。

我尝试了几种方法,但所有方法最终都以拒绝或承诺等待和测试时间出现。

例如 -

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) )

我问了一个类似的问题,与puppeteer 在这里但是,如果可能的话,我正在尝试使用page.locator以更多的剧作家方式对此进行处理。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嘿看小鸭子会跑 2025-02-12 18:18:46
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())
}
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())
}
长发绾君心 2025-02-12 18:18:46

在最新的剧作家中,您只需单个步骤即可获取和验证文本:

expect(page.locator('.my-table-row').nth(2)).toContainText('Steve')

In latest playwright, you can simply do to get and verify text in single step:

expect(page.locator('.my-table-row').nth(2)).toContainText('Steve')
不爱素颜 2025-02-12 18:18:46
let arrayName = new Array<string>();
let count = await page.locator('tr').count();
for (let i = 0; i < count; i++){
  let name : string;
  name  = await page.locator('tr').nth(i).locator('td').nth(0).locator(':scope').allInnerTexts();
  name = name.toString();
  arrayName.push(name);
}
console.log(arrayName);

nb:nth(0) - &gt;需要根据您需要从中获取数据的列来自定义

let arrayName = new Array<string>();
let count = await page.locator('tr').count();
for (let i = 0; i < count; i++){
  let name : string;
  name  = await page.locator('tr').nth(i).locator('td').nth(0).locator(':scope').allInnerTexts();
  name = name.toString();
  arrayName.push(name);
}
console.log(arrayName);

NB : nth(0) -> need to be customized based on the column which you need to get data from

噩梦成真你也成魔 2025-02-12 18:18:46

使用剧作家1.21,引入了一种新方法,称为:scope。您可以阅读有关它的更多信息在这里。使用此情况,您可以这样优化您的代码:

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)
})

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)
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文