【问题标题】:Puppeteer: find iframe by classPuppeteer:按类查找 iframe
【发布时间】:2020-10-15 18:39:52
【问题描述】:

我正在将 Puppeteer 与 Jest 一起使用,我正在尝试使用此函数获取 iframe 元素:

const frame = await page
  .frames()
  .find(f => f.name() === 'iframe-class');

问题是:有没有办法通过他的类而不是 name 属性来获取 iframe?

我无权访问这个 iframe 来插入新属性,所以我需要按类选择。

【问题讨论】:

    标签: javascript node.js jestjs puppeteer jest-puppeteer


    【解决方案1】:

    您可以使用contentFrame 函数。

    const elementHandle = await page.$('.iframe-class');
    const frame = await elementHandle.contentFrame();
    

    【讨论】:

      【解决方案2】:

      考虑使用文档查询选择器来查找您的<iframe class="external-data">

      document.querySelectorAll('.external-data');
      

      【讨论】:

        猜你喜欢
        • 2021-09-02
        • 2019-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多