Javascript Puppeter-迭代表行并选中特定行的复选框
我有一个表格,其中第一列是每一行的复选框,我想选中该行的复选框,该行在所述行的特定列中包含特定文本 示例Javascript Puppeter-迭代表行并选中特定行的复选框,javascript,html-table,puppeteer,browser-automation,web-testing,Javascript,Html Table,Puppeteer,Browser Automation,Web Testing,我有一个表格,其中第一列是每一行的复选框,我想选中该行的复选框,该行在所述行的特定列中包含特定文本 示例 X | Dave | Smith X | Bill | Jones Iterate through all rows and then check the box in the first column if the row has "Jones" for the last name column (col 3) const result = await page.$$eval('#ex
X | Dave | Smith
X | Bill | Jones
Iterate through all rows and then check the box in the first column if the row has "Jones" for the last name column (col 3)
const result = await page.$$eval('#example-table tr', rows => {
return Array.from(rows, row => {
const columns = row.querySelectorAll('td');
return Array.from(columns, column => column.innerText);
});
});
console.log(result[1][2]); // "C2"
网站上有一些很好的示例,可以从表中抓取数据并输出所述数据,但我希望实际遍历每一行,然后根据需要对给定单元格中的元素执行操作
刮取示例
X | Dave | Smith
X | Bill | Jones
Iterate through all rows and then check the box in the first column if the row has "Jones" for the last name column (col 3)
const result = await page.$$eval('#example-table tr', rows => {
return Array.from(rows, row => {
const columns = row.querySelectorAll('td');
return Array.from(columns, column => column.innerText);
});
});
console.log(result[1][2]); // "C2"
上面的示例非常适合于在2D数组中提取数据,但我希望遍历每一行,在特定列中查找特定文本,然后在列0中的元素上执行单击操作以选中该框
我使用的是木偶演员,对javascript还比较陌生,所以如果有人能提供任何指导,我将不胜感激
我尝试过很多类似的失败尝试
const rows = await page.$$eval('table tbody tr', (rows) => {
const cols = await page.$$eval('table tbody tr td', (cols) => {
for (let i=0; i<rows.length; i++) {
for (let j=0; j<cols.length; j++) {
// if row[i] col[2] == desired text
// then check box in row[i] col[0]
}
}
})
}
const rows=等待页面。$$eval('table tbody tr',(rows)=>{
const cols=等待页面。$$eval('table tbody tr td',(cols)=>{
对于(设i=0;i,迭代tr可能更好:
await page.evaluate(() => {
// find the tr with Jones
let tr = [...document.querySelectorAll('tr')].find(tr => tr.innerText.match(/Jones/))
if(tr){
// find the checkbox and click it
let cb = tr.querySelector('input[type="checkbox"]')
if(cb) {
cb.click()
}
}
})
迭代tr可能更好:
await page.evaluate(() => {
// find the tr with Jones
let tr = [...document.querySelectorAll('tr')].find(tr => tr.innerText.match(/Jones/))
if(tr){
// find the checkbox and click it
let cb = tr.querySelector('input[type="checkbox"]')
if(cb) {
cb.click()
}
}
})
这看起来正是我想要的,但现在我得到了这个错误。错误:评估失败:引用错误:toConsumableArray在木偶演员评估脚本中没有定义。我正在使用Babel并传输到es5,所以这是我的问题的一部分吗?非常感谢任何帮助。再次感谢!听起来像是这样,但我没有看看你为什么会在这里使用babel。这看起来正是我想要的,但现在我得到了这个错误。错误:评估失败:引用错误:toConsumableArray在木偶演员评估脚本3:19中没有定义。我正在使用babel并传输到es5,这是我的问题的一部分吗?非常感谢你的帮助。再次感谢你在!听起来像,但我不明白你为什么要在这里使用巴贝尔。