简体   繁体   English

在 Cypress 中获取本机 HTML 元素

[英]Get native HTML element in Cypress

How do I get an underlying native HTMLElement (or Element or Node ...) from a Cypress query?如何从赛普拉斯查询中获取底层原生HTMLElement (或ElementNode ...)?

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 .我想访问el中的本机HTMLElement实例。
I have tried el[0] but the result is of type any .我试过el[0]但结果是any类型。

In cypress, first() is a command that will be chained to .get() and retry for that whole selection until timeout.在 cypress 中, first()是一个命令,它将被链接到.get()试整个选择,直到超时。 That is why you can't really obtain the native element from it.这就是为什么您不能真正从中获取本机元素的原因。

You can however yield the command, and access the element inside .then() by passing a function to it.但是,您可以生成命令,并通过将 function 传递给它来访问 .then .then()中的元素。

For example, from the Cypress documentation on this EXACT question :例如,来自赛普拉斯关于这个 EXACT 问题的文档

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM