【发布时间】:2021-05-16 07:28:24
【问题描述】:
这个问题肯定被问过很多次了,但是我到处找,没有一个答案对我有用。
所以我有以下Div:
<div class="dataTables_info" id="dt-card-entry_info" role="status" aria-live="polite">
Showing 1 to 20 of 761,871 entries
<span class="select-info">
<span class="select-item">
1 row selected
</span>
<span class="select-item">
</span>
<span class="select-item">
</span>
</span>
</div>
我正在尝试获取父 div 中的文本:显示 761,871 个条目中的 1 到 20 个
我试过了:
const text = await page.$eval('div#dt-card-entry_info.dataTables_info', el => el.textContent)
还有
const text = await page.evaluate(() => {
const el = document.querySelector('#dt-card-entry_info')
return el.innerText
})
从浏览器控制台,这可行:
$('#dt-card-entry_info').text()
还有这个:
$('#dt-card-entry_info')[0].innerText
或者这个:
$('#dt-card-entry_info')[0].textContent
【问题讨论】:
-
你可以试试
document.getElementById('dt-card-entry_info')developer.mozilla.org/en-US/docs/Web/API/Document/… 浏览器控制台有可用的jQuery,但puppititeer没有iirc -
我如何获取文本?
-
我怀疑它与嵌套的
span元素有关。
标签: javascript html node.js puppeteer innertext