Search code examples
puppeteer

Puppeteer - How to fill form that is inside an 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.


Solution

  • 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 });