这个问题肯定已经被问过多次了,但是我到处都是,没有一个答案对我有用。
因此,我有以下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
您可以使用
您想要文本内容,请使用:
var res = document.getElementById('dt-card-entry_info').textContent;
您的方法可以这样使用:
const text = await page.evaluate(() => {
const el = document.getElementById('dt-card-entry_info');
return el.textContent;
})
我不喜欢const
def中的await pageEval ,因此我将在eval范围之外进行更改。
这是因为pageEval是一个Promise,因此您将需要依次返回一个字符串内容的Promise。在这里阅读更多
let text = '';
await page.evaluate(() => {
const el = document.getElementById('dt-card-entry_info');
text = el.textContent;
})
console.log(text);
您可以在这里工作:https : //jsfiddle.net/9s4zxvLk/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句