【问题标题】:How to use a selector finding an frame (iframe) using Playwright如何使用选择器使用 Playwright 查找框架(iframe)
【发布时间】:2020-10-17 03:10:42
【问题描述】:

我有一个小问题,我找不到使用 Microsoft Playwright 框架的答案。根据文档 (https://playwright.dev/#version=v1.1.1&path=docs%2Fcore-concepts.md&q=pages-and-frames),您可以使用以下代码获取 iframe

const frame = page.frame('frame-login');

但是如何使用选择器来查找和交互 iframe?我需要使用 css-selector 来查找我的 iframe,因为它没有 id。

任何帮助表示赞赏

【问题讨论】:

    标签: javascript node.js playwright


    【解决方案1】:

    您可以使用elementHandle.contentFrame()

    await page.waitForSelector('.class-name')
    
    const elementHandle = await page.$('.class-name')
    const frame = await elementHandle.contentFrame()
    

    从那一刻起,您可以与<iframe> 的内容进行交互,例如:await frame.<method_name>

    【讨论】:

      【解决方案2】:

      你可以得到ElementHandle调用$然后调用contentFrame函数:

      const handle = await page.$('.frame');
      const contentFrame = await handle.contentFrame();
      

      【讨论】:

        猜你喜欢
        • 2021-01-25
        • 2021-03-19
        • 1970-01-01
        • 2019-05-20
        • 1970-01-01
        • 2015-12-08
        • 2021-06-16
        • 2021-01-23
        • 2021-11-11
        相关资源
        最近更新 更多