node.js - 如何使用 Codeceptjs 和 Puppeteer 根据 col3 中的文本在 col1 中选中复选框
问题描述
Codeceptjs 和 Puppeteer 相当新。想知道是否有人可以提供一些关于与表格中的元素进行交互的最佳实践的见解?
我有一个表,其中每个数据行的第一列是一个复选框。
我想通过其中一列中的文本来识别表中的一行。
我已经构建了如下所示的方法来删除记录,因为在项目的属性中有一个删除按钮,只需单击该行即可显示。
deleteSite(id) {
I.click(locate('tr').withText(id));
I.click(this.button.delete);
I.wait(3);
},
我试图弄清楚如何以这样一种方式构造它,即我仍然可以使用定位找到我所追求的行,然后还单击该行第一列中的复选框。
如果有更好的方法/方法来做到这一点,我会全神贯注。
任何帮助将不胜感激。
谢谢,鲍勃
解决方案
After many trials and mistakes on my part, this is actually quite easy to do and works well.
Here is what I've come up with. If others have suggestions for how this can be improved, I am always open to that.
Locate a row, tick a check box in col1 of identified row
async selectSiteById(siteId) {
// Get total number of rows in table (body)
const totalRows = await I.grabAttributeFrom('tbody tr');
// Loop through all rows of the table
for (let i = 0; i < totalRows.length; i++) {
// Get the value of the site ID and store to str
let str = await I.grabTextFrom(`tbody tr:nth-child(${i+1}) td:nth-child(2)`);
// Click the checkbox in col1 for the identified row
if (str === siteId) {
within(`tbody tr:nth-child(${i+1}) td:nth-child(1) span span`, () => {
I.click('input');
})
}
}
},
推荐阅读
- twitter - 我不能在 Nim 中使用 twitter api
- sql - 如何使用子序列列重新编号列序列值
- matrix - 在 javafx 中创建一个随机二进制矩阵
- php - 在 Ubuntu 上用 PHP 重启 Apache
- linux - 比较文件内容并删除较短的内容
- git - 让每个人的 git 忽略已经跟踪的文件(但不要从存储库中删除)
- php - 提交 html 表单时如何从 URI 中省略变量名?
- python - 类型错误:+ 不支持的操作数类型:'int' 和 'list' 尝试拆分文本文件时
- html - 在我的图标栏中为我的图标添加文本 (HTML)
- android - Android:通过使用标签而不是 ID 查找容器来添加片段