javascript - 遍历通过 XPath 选择的元素的子元素
问题描述
我正在尝试遍历第一个 div 元素的子元素并获取 textContent 属性。我可以只使用常规的 querySelectorAll ($$),然后遍历该数组。但是,我需要它遍历为使用 XPath 选择的子项创建的数组。现在我有这个代码:
<html>
<head>
</head>
<body>
<div>
<div>This is the first div</div>
<div>This is the second div</div>
<div>This is the third div</div>
</div>
</body>
</html>
这是 JavaScript
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
headless: false,
});
const page = await browser.newPage();
await page.goto('http://127.0.0.1:5500/index.html');
await page.waitForXPath(('/html/body/div/div[2]'))
const [liveDiv] = await page.$x('/html/body/div/div[2]');
const [liveDivParent] = await liveDiv.$x('..');
const childArr = await liveDivParent.$x('/*')
childArr.forEach(async (el) => {
const textC = await el.getProperty('textContent');
const textJ = await textC.jsonValue();
console.log(textJ)
})
await browser.close();
})();
现在的问题是,当它将所有内容放入单个对象时,forEach 仅控制台一次记录所有内容
hello
Wasasp
LOL
不是在每个循环中列出它们,而是在早上 5 点,我已经筋疲力尽,但非常沮丧,我无法弄清楚。如果您有任何提示或建议,我非常感谢!感谢:D
解决方案
推荐阅读
- excel - 交替运行时未设置 Word 宏对象变量或块变量
- oh-my-zsh - 在 robbyrussell 主题的 ohmyzsh 控制台上添加时间
- python - IN 语句的 COUNT(从范围计数,而不是从表中计数)
- mongodb - Mongodb:以正确的顺序使用 $graphLookup、$reduce 和 $concat 构建类别 url
- scala - 将双列数组动态转换为嵌套火花数据框中的多列
- regex - 仅允许使用 mod_rewrite 的虚拟主机中的主机
- python - Python将输出写入目录中的文件
- python - 如何从具有多个值的 JSON 文件中提取数据
- ssl - 无法找到请求目标的有效证书路径 - 签名证书
- ruby-on-rails - Rails:将一个时区的未来会议时间转换为另一个时区