获取循环内的元素。

huangapple go评论53阅读模式
英文:

Cypress get elements inside each loop

问题

我有一个表格,第一列的单元格包含一个 div 和图片。
我需要点击 div。
我还需要对第三列执行一些测试。
所以我给了选择器直到 td,并没有包括 div。

            if(index === 10) {
                // 如何获取 div 并点击
            }
        })```

我知道我可以用下面的方式实现:
```cy.get("fcsky-shared-table cdk-virtual-scroll-viewport mat-table tr.mat-row td:nth-child(1) div").eq(10).click()```

我想知道是否有一种方式可以获取每个内部元素:
```cy.get("f-shared-table cdk-virtual-scroll-viewport mat-table tr.mat-row td:nth-child(1)").each(($el, index, $list) => {
            if(index === 10) {
                cy.wrap($el).get("div").click() // 不起作用
                cy.wrap($el).children("div").click() // 不起作用
            }
        })```

<details>
<summary>英文:</summary>

I have a table , the first column cell contains a div and image .
I need to click on div.
I also need to perform some test on third column.
So i gave the selector till td and didn&#39;t include div. 


cy.get("f-shared-table cdk-virtual-scroll-viewport mat-table tr.mat-row td:nth-child(1)").each(($el, index, $list) => {
if(index === 10) {
// how to get the div to click it
}
})


I know i can achieve it using

cy.get("fcsky-shared-table cdk-virtual-scroll-viewport mat-table tr.mat-row td:nth-child(1) div").eq(10).click()


I was wondering if there is any way where i can get the elements inside each

cy.get("f-shared-table cdk-virtual-scroll-viewport mat-table tr.mat-row td:nth-child(1)").each(($el, index, $list) => {
if(index === 10) {
cy.wrap($el).get("div").click() // not working
cy.wrap($el).children("div").click() // not working
}
})



</details>


# 答案1
**得分**: 2

你可能会发现,将行用作索引更容易,在第11行内查找`<div>`,类似于:

```js
cy.get('f-shared-table cdk-virtual-scroll-viewport mat-table tr.mat-row')
  .each(($tr, index) => {
    if (index === 10) {
      cy.wrap($tr).within(() => {
        // 以下命令仅限于第11行
        cy.get('td').first().find('div').click() 
      })        
    }
  })
英文:

You may find it easier to use the row as index, and within the 11th row find the &lt;div&gt;, something like:

cy.get(&#39;f-shared-table cdk-virtual-scroll-viewport mat-table tr.mat-row&#39;)
  .each(($tr, index) =&gt; {
    if (index === 10) {
      cy.wrap($tr).within(() =&gt; {
        // the following command is limited to eleventh row
        cy.get(&#39;td&#39;).first().find(&#39;div&#39;).click() 
      })        
    }
  })

huangapple
  • 本文由 发表于 2023年3月21日 00:08:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定