我有一個 Grid 組件,其中包括 24 個 div,在每個 div 內部我需要取值。
這個值實際上到達<p>,那么最好的方法是什么?
下面是應用程式影像。我會很感激一個例子。

uj5u.com熱心網友回復:
您可能會做一些事情,例如將資料存盤在賽普拉斯鏈之外的物件或陣列中。沒有代碼示例,這是我最好的猜測。
cy.get('grid').within(() => { // cy.within() searches only within yielded element
const data = {}; // create data object
cy.get('div').each(($div, index) => { // cy.each() allows us to iterate through yielded elements
data[index] = $div.text() // or possibly some other JQuery command to get the value
// additionally, could go without the index at all and make `data` an array.
}).then(() => {
// whatever needs to be done with `data`, wrapped in `then` to make sure data is populated correctly.
});
});
uj5u.com熱心網友回復:
您可以為此添加 useeach以回圈遍歷元素,然后執行進一步的操作:
cy.get('.chakra-stack')
.find('p')
.each(($ele) => {
cy.log($ele.text().trim()) //Logs all the texts one by one
})
uj5u.com熱心網友回復:
只需將p選擇器添加到您的cy.get()命令中
cy.get('div.chakra-stack p') // access <p> within <div>
.each($el => {
cy.wrap($el).invoke('text')
.then(text => {
...
})
})
獲取文本之前的值
cy.get('div.chakra-stack p') // access <p> within <div>
.each($el => {
cy.wrap($el)
.prev() // element with value
.invoke('text')
.then(value => {
...
})
})
uj5u.com熱心網友回復:
像這樣通過文本標簽訪問值
const values = {}
cy.get('div.chakra-stack p')
.each($el => {
const frase = $el.text()
cy.wrap($el).prev().invoke('text')
.then(value => values[frase] = value)
})
.then(() => {
// values = {'shield': 1, 'session': 2, ...}
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/460047.html
