【问题标题】:Why am I not able to navigate through iFrames using Apify/Puppeteer?为什么我无法使用 Apify/Puppeteer 浏览 iFrame?
【发布时间】:2020-12-01 16:27:05
【问题描述】:

我正在尝试使用 Puppeteer 来操作带有 iFrame 的网站形式。我尝试了不同的方法来访问特定的 iFrame,甚至计算网站中的 iFrame,但均未成功。

为什么 Puppeteer 的对象不能识别我试图浏览的页面的 iFrame/子框架?

其他页面也会出现这种情况,例如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();
});

【问题讨论】:

    标签: node.js puppeteer apify


    【解决方案1】:

    也许您会在下面找到一些有用的灵感。

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

    试一试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-02
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 2022-12-28
      相关资源
      最近更新 更多