【问题标题】:getAttribute is not available on the ElementHandlegetAttribute 在 ElementHandle 上不可用
【发布时间】:2020-08-05 05:58:04
【问题描述】:

我用的是playwright 0.13.0版,

我有一个ElementHandle 的实例,但getAttribute 函数不可用,调用它会引发错误,指出getAttribute 不是函数:

await myElem.getAttribute('src')

我用调试器仔细检查过,函数不在实例上。

另外,ElementHandle 没有等效的 page.evaluate 函数

【问题讨论】:

    标签: javascript node.js playwright


    【解决方案1】:

    这对我有用:

    const attr = await page.$eval('.your-locator-class', el => el.src)
    

    【讨论】:

      【解决方案2】:

      对于这方面的新手,更高版本的 Playwright 支持 OP 使用的 API。见:https://playwright.dev/docs/api/class-elementhandle#elementhandlegetattributename

      【讨论】:

        【解决方案3】:

        您可以将它作为参数传递给page.evaluate 函数:

        await page.evaluate(el => el.getAttribute('src'), myElem);
        

        await myElem.evaluate(node => node.getAttribute('src');
        

        【讨论】:

        • 我个人认为下面的语法更好读一些。 await myElem.evaluate(node => node.getAttribute('src');
        猜你喜欢
        • 1970-01-01
        • 2012-03-31
        • 2023-01-13
        • 1970-01-01
        • 2019-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多