如何从剧作家中的表格中获取特定列的文本
我正在使用剧作家进行E2E测试。我拥有的问题陈述是我想获取表特定列的所有行的文本内容。所讨论的表就像在此处显示的表一样 - https:// https://ant.design/components/components/table/
(尽管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/
(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在最新的剧作家中,您只需单个步骤即可获取和验证文本:
In latest playwright, you can simply do to get and verify text in single step:
nb:nth(0) - >需要根据您需要从中获取数据的列来自定义
NB : nth(0) -> need to be customized based on the column which you need to get data from
使用剧作家1.21,引入了一种新方法,称为
:scope
。您可以阅读有关它的更多信息在这里。使用此情况,您可以这样优化您的代码: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: