【问题标题】:Playwright Select frame with dynamic name具有动态名称的剧作家选择框架
【发布时间】:2021-10-16 06:31:12
【问题描述】:

我需要访问 playwright 中具有自动生成名称的 iframe。

iframe 的名称总是以“__privateStripeFrame”为前缀,然后是一个随机生成的数字

如何使用 page.frame({name: }) 访问框架?

从文档看来我不能使用正则表达式!

【问题讨论】:

    标签: javascript typescript dom e2e-testing playwright


    【解决方案1】:

    frameSelector 不需要由名称指定。 尝试使用 xpathcontains - 这适用于 W3 示例页面:

        await page.goto('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe');
        await page.frame("//iframe[contains(@title,'W3s')]");
    

    如果您想要更通用的方法 - 您还可以使用 page.frames()。 这将返回一个帧数组,您可以遍历并找到您需要的帧。

    这对我有用:

        let myFrames = page.frames();
        console.log("#frames: " + myFrames.length)
        myFrames.map((f) => console.log(f.name()));
    

    (W3S 不是最好的演示站点,因为有很多嵌套框架 - 但它会输出具有名称的顶级框架)

    输出:

    iframe结果

    __tcfapiLocator

    __uspapiLocator

    __tcfapiLocator

    __uspapiLocator

    【讨论】:

    • 我使用了 page.frames() 解决方案。那个人完成了这项工作,谢谢!
    猜你喜欢
    • 2021-02-25
    • 1970-01-01
    • 2021-02-09
    • 2022-06-30
    • 1970-01-01
    • 2022-11-18
    • 2021-10-08
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多