Puppeteer:获取元素的文本内容 / inner HTML
问题:
你想使用 puppeteer 自动化测试网页。你需要获取某些元素的文本或 inner HTML,例如
element_example.html
<div id="mydiv">
</div>在页面上。
解决方案
puppeteer_get_inner_text.js
// 获取内部文本
const innerText = await page.evaluate(() => document.querySelector('#mydiv').innerText);
// 获取内部 HTML
const innerHTML = await page.evaluate(() => document.querySelector('#mydiv').innerHTML);注意 .innerText 包含子元素的文本。你可以在 page.evaluate(...) 内使用完整的 DOM API。你可以使用任何 CSS 选择器作为 document.querySelector(...) 的参数。
Check out similar posts by category:
Javascript, Puppeteer
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow