Puppeteer - 如何填写 iframe 内的表单? [英] Puppeteer - How to fill form that is inside an iframe?
本文介绍了Puppeteer - 如何填写 iframe 内的表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须填写 iframe 内的表单,这里是示例页面.我无法通过简单地使用 page.focus()
和 page.type()
来访问.我试图通过使用 const formFrame = page.mainFrame().childFrames()[0]
来获取表单 iframe,这可行,但我无法真正与表单 iframe 交互.
I have to fill out a form that is inside an iframe, here the sample page. I cannot access by simply using page.focus()
and page.type()
. I tried to get the form iframe by using const formFrame = page.mainFrame().childFrames()[0]
, which works but I cannot really interact with the form iframe.
推荐答案
我自己想出来的.这是代码.
I figured it out myself. Here's the code.
console.log('waiting for iframe with form to be ready.');
await page.waitForSelector('iframe');
console.log('iframe is ready. Loading iframe content');
const elementHandle = await page.$(
'iframe[src="https://example.com"]',
);
const frame = await elementHandle.contentFrame();
console.log('filling form in iframe');
await frame.type('#Name', 'Bob', { delay: 100 });
这篇关于Puppeteer - 如何填写 iframe 内的表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文