javascript - getAttribute is not available on the ElementHandle - Stack Overflow

I'm using playwright version 0.13.0, I have an instance of ElementHandle, but the getAttribute fun

I'm using playwright version 0.13.0,

I have an instance of ElementHandle, but the getAttribute function is not available, calling it throws an error saying getAttribute is not a function:

await myElem.getAttribute('src')

I double-checked with the debugger, the function is not on the instance.

Also, there's no equivalent of page.evaluate function for ElementHandle

I'm using playwright version 0.13.0,

I have an instance of ElementHandle, but the getAttribute function is not available, calling it throws an error saying getAttribute is not a function:

await myElem.getAttribute('src')

I double-checked with the debugger, the function is not on the instance.

Also, there's no equivalent of page.evaluate function for ElementHandle

Share Improve this question edited Apr 22, 2020 at 11:05 hardkoded 21.8k3 gold badges61 silver badges74 bronze badges asked Apr 22, 2020 at 9:08 Don BoxDon Box 3,3464 gold badges30 silver badges62 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

You can pass it as an argument to the page.evaluate function:

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

or

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

For new ers to this, later versions of Playwright support the API used by OP. See: https://playwright.dev/docs/api/class-elementhandle#elementhandlegetattributename

This worked for me:

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

You can easily solved it using playwright native getter - .getAttribute('someAttribute');

For example you can write something like that:

const locator = await page.locator('[name="SomeLocator"]').getAttribute('content');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744173388a4561643.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信