javascript - Puppeteer 选择表格单元格的第 n 个子项中的链接
问题描述
我有一个功能:
const tableData = await page.evaluate(() => {
const tds = Array.from(document.querySelectorAll('table tbody tr td'))
for (var i = 0; i < tds.length; i++) {
...
...
availableDates.push([i,dateString,day])
}
return availableDates
}
这个函数循环一个表格的所有单元格,我根据一些条件过滤一些日期。
到现在为止还挺好。单元格包含我要单击的 href。该数组包含我要单击第一个数组项的单元格的编号,因此我尝试了:
await page.focus('table tbody tr td:nth-child('+tableData[0][0]+') a' )
await page.keyboard.type('\n');
但没有运气。我收到以下错误消息:
(node:81325) UnhandledPromiseRejectionWarning: Error: No node found for selector: table tbody tr td:nth-child(109) a
.......
我必须改变什么?谢谢。
解决方案
这不是 CSS 选择器的工作方式。您不能做也不能td:nth-child(109)
期望选择不同表格行中的元素。我会尝试使用.click()
,通过page.click
或page.evaluate(() => element.click);
打字\n
似乎是一个错误的方向,明确地调用点击——这是我的建议。
用于page.click
:https ://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pageclickselector-options
当谈到实际找到正确的单元格时,我会在更高范围内使用辅助数组(闭包),在那里添加所有内容(一维数组),然后您可以通过数字引用它们,例如您尝试使用nth-child
方法。
推荐阅读
- git - 查找也存在/被推送到上游的最后一个本地 git 提交
- ios - 通过将应用程序发送到登录屏幕 ios 来处理异常
- excel - Excel VBA:循环遍历行,同时按顺序分隔日期跨度
- java - 使用流优化列表遍历
- python - 在两种情况下,Python 系统调用 (os.system) 不能按预期使用相同的字符串
- javascript - 如何在 .php 文件中链接外部 .js 文件
- c# - Linq Query 将表连接到列表
对象以查找不在表中的任何字符串 - ansible - 针对具有不同配置的多个主机
- r - 有没有一种简单的方法可以知道哪些函数矢量化,哪些不矢量化?
- c# - ComboBox SelectedItem 绑定