admin管理员组文章数量:1193330
I have an interesting puppeteer problem that I'm not sure how to solve.
I have a webpage with an iframe
on it. To get the iframe handle I do the following:
const iframeHandle = await page.$('iframe')
To get the contentFrame I simply run:
const frame = await iframeHandle.contentFrame()
However, this returns null
.
I printed out the iframeHandle
to make sure I got the correct element, and it does grab the correct iframe:
console.dir(iframeHandle)
_remoteObject: {
type: 'object',
subtype: 'node',
className: 'HTMLIFrameElement',
description: 'iframe',
objectId: '{"injectedScriptId":5,"id":6}'
},
Does anyone know how I can get the content of the iframe?
I have an interesting puppeteer problem that I'm not sure how to solve.
I have a webpage with an iframe
on it. To get the iframe handle I do the following:
const iframeHandle = await page.$('iframe')
To get the contentFrame I simply run:
const frame = await iframeHandle.contentFrame()
However, this returns null
.
I printed out the iframeHandle
to make sure I got the correct element, and it does grab the correct iframe:
console.dir(iframeHandle)
_remoteObject: {
type: 'object',
subtype: 'node',
className: 'HTMLIFrameElement',
description: 'iframe',
objectId: '{"injectedScriptId":5,"id":6}'
},
Does anyone know how I can get the content of the iframe?
Share Improve this question asked Nov 28, 2020 at 12:24 Dirk HoekstraDirk Hoekstra 1,05114 silver badges17 bronze badges 1- See also this answer in Puppeteer - How to fill form that is inside an iframe? – ggorlen Commented Aug 10, 2021 at 19:10
6 Answers
Reset to default 20The problem is with browser startup options. Add the following to "args":
const browser = await puppeteer.launch({
headless: false,
args: [
'--disable-web-security',
'--disable-features=IsolateOrigins,site-per-process'
]
});
I was only able to get the frame by using page.frames()
and then looping through to find the correct one. Accessing it in this way didn't require calling .contentFrame()
at all.
const frames = await page.frames();
const frame = frames.find(f => f.url().includes('example.com'));
Okay, My answer may seem a bit strange and un-elegant but it works.. the idea is to evaluate code PURELY on the tab side and get data on your end just by seeing what certain things from the tab RETURN..
(async()=>{
const src="https://github.com"
const browser = await puppeteer.launch({executablePath: myExecutablePath, headless: false});
const page = await browser.newPage();
await page.goto(src);
await page.evaluate(`window.iframe=document.createElement('iframe');window.iframe.src='${src}';document.body.appendChild(window.iframe)`); //you get the idea, i made the iframe on the browser side
const iframe=await page.evaluate(`window.iframe`);
console.log(iframe);
console.log("Above was your side iframe\nBelow is window side iframe");
console.log(await page.evaluate(`window.iframe`));
})()
Try:
const elementHandle = await page.$('iframe');
const frame = await elementHandle.contentFrame();
const elementHandle = await page.$('#iframe');
const frame = await elementHandle.contentFrame();
await frame.click('#introAgreeButton');
use Chromium 89.0.4370, it work for me.
本文标签: javascriptPuppeteer iframe contentFrame returns nullStack Overflow
版权声明:本文标题:javascript - Puppeteer iframe contentFrame returns null - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738459746a2087963.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论