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.
The frames in an html code are represented by the frames/iframe tag. Puppeteer can handle frames by switching from the main page to the frame. To work with elements inside a frame, first, we have to identify the frame with the help of locators. The method contentFrame is used to access the elements inside the frame.
An iframe is used to embed another document inside a html page. If the form is to be submitted to an iframe within the form page, then it can be easily acheived using the target attribute of the tag. Set the target attribute of the form to the name of the iframe tag.
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 });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With