Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer - How to fill form that is inside an iframe?

Tags:

puppeteer

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.

like image 284
Raza Avatar asked Oct 02 '17 15:10

Raza


People also ask

How do you handle iframe in puppeteer?

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.

How do I load an iframe form?

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.


1 Answers

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 }); 
like image 162
Raza Avatar answered Sep 20 '22 18:09

Raza