最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get native HTML element in Cypress - Stack Overflow

programmeradmin4浏览0评论

How do I get an underlying native HTMLElement (or Element or Node...) from a Cypress query?

const el = cy.get('.foo').children().first()
//    ^ this is of type Cypress.Chainable<JQuery<HTMLElement>>

I would like to access the native HTMLElement instance in el.
I have tried el[0] but the result is of type any.

How do I get an underlying native HTMLElement (or Element or Node...) from a Cypress query?

const el = cy.get('.foo').children().first()
//    ^ this is of type Cypress.Chainable<JQuery<HTMLElement>>

I would like to access the native HTMLElement instance in el.
I have tried el[0] but the result is of type any.

Share Improve this question edited Nov 15, 2019 at 15:45 P Varga asked Nov 15, 2019 at 15:16 P VargaP Varga 20.2k13 gold badges77 silver badges118 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 15 +300

In cypress, first() is a mand that will be chained to .get() and retry for that whole selection until timeout. That is why you can't really obtain the native element from it.

You can however yield the mand, and access the element inside .then() by passing a function to it.

For example, from the Cypress documentation on this EXACT question:

cy.get('.foo').then(($el) => {
  const el = $el.get(0) //native DOM element
})
发布评论

评论列表(0)

  1. 暂无评论