PuppeteeriframecontentFrame返回null
我有一个有趣的木偶问题,我不知道如何解决。
我有一个网页,上面有一个iframe。要获得 iframe 句柄,我执行以下操作:
const iframeHandle = await page.$('iframe')
要获取 contentFrame 我只需运行:
const frame = await iframeHandle.contentFrame()
但是,这将返回null。
我打印出来iframeHandle以确保我得到了正确的元素,它确实抓取了正确的 iframe:
console.dir(iframeHandle)
_remoteObject: {
type: 'object',
subtype: 'node',
className: 'HTMLIFrameElement',
description: 'iframe',
objectId: '{"injectedScriptId":5,"id":6}'
},
有谁知道如何获取 iframe 的内容?
回答
问题在于浏览器启动选项。将以下内容添加到“args”:
const browser = await puppeteer.launch({
headless: false,
args: [
'--disable-web-security',
'--disable-features=IsolateOrigins,site-per-process'
]
});