简体   繁体   中英

Why am I not able to navigate through iFrames using Apify/Puppeteer?

I'm trying to manipulate forms of sites w/ iFrames in it using Puppeteer. I tried different ways to reach a specific iFrame, or even to count iFrames in a website, with no success.

Why isn't Puppeteer's object recognizing the iFrames / child frames of the page I'm trying to navigate through?

It's happening with other pages as well, such as https://www.veiculos.itau.com.br/simulacao

const Apify = require('apify');
const sleep = require('sleep-promise');

Apify.main(async () => {
    // Launch the web browser.
    const browser = await Apify.launchPuppeteer();

    // Create and navigate new page
    console.log('Open target page');
    const page = await browser.newPage();
    await page.goto('https://www.credlineitau.com.br/');
    await sleep(15 * 1000);
    for (const frame in page.mainFrame().childFrames()) {
        console.log('test');
    }
await browser.close();
});

Perhaps you'll find some helpful inspiration below.

const waitForIframeContent = async (page, frameSelector, contentSelector) => {
    await page.waitForFunction((frameSelector, contentSelector) => {
        const frame = document.querySelector(frameSelector);
        const node = frame.contentDocument.querySelector(contentSelector);
        return node && node.innerText;
    }, {
        timeout: TIMEOUTS.ten,
    }, frameSelector, contentSelector);
};

const $frame = await waitForSelector(page, SELECTORS.frame.iframeNode).catch(() => null);

if ($frame) {
    const frame = page.frames().find(frame => frame.name() === 'content-iframe');
    const $cancelStatus = await waitForSelector(frame, SELECTORS.frame.membership.cancelStatus).catch(() => null);
    await waitForIframeContent(page, SELECTORS.frame.iframeNode, SELECTORS.frame.membership.cancelStatus);
}

Give it a shot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM