My test html:
<div id="mainBlock">
<div class="underBlock">
Hello!
</div>
</div>
我试着得到div的内容与类underBlock
这样:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
headless:false,
});
const page = await browser.newPage();
let response = await page.goto('http://localhost/TestPup/Index.html');
let block = await page.waitForXPath("//div[contains(@class,'underBlock')]")
let frame = await block.contentFrame()
console.log(frame.content())
await browser.close();
})();
but I got error:
TypeError: Cannot read property 'content' of null
据我了解,elementHandle.contentFrame()
只返回iframe
元素的一个帧,你有一个常规的div
,它包含在主帧中,也就是在page
中,里面没有帧。