我在柏树中有以下代码:
for(let i = 0; i < lengthOfRow; i++){
if (
Cypress.$(':nth-child(1) > :nth-child(6) > .index_textBlock_20DCh').eq(0).text().length == 0 &&
Cypress.$(':nth-child(1) > :nth-child(7) > .index_textBlock_20DCh').eq(1).text().length == 0 &&
Cypress.$(':nth-child(1) > :nth-child(8) > .index_textBlock_20DCh').eq(2).text().length == 0
) {
//Do Something
cy.get('tbody > :nth-child(1) > :nth-child(6)').click()
} else if (
Cypress.$(':nth-child(2) > :nth-child(6) > .index_textBlock_20DCh').eq(0).text().length == 0 &&
Cypress.$(':nth-child(2) > :nth-child(7) > .index_textBlock_20DCh').eq(1).text().length == 0 &&
Cypress.$(':nth-child(2) > :nth-child(8) > .index_textBlock_20DCh').eq(2).text().length == 0
){
//Do Something
cy.get('tbody > :nth-child(2) > :nth-child(6)').click() }
//another more conditions在这里,我正在检查块为空(否则转到elseIf),但是它一开始已经失败了(错误:应该是空的,但是找到了一些东西)。那么,我的问题是,这个代码有什么问题,柏树是否支持使用&&?
HTML:
<tr class = ="index_tr_2P">
<td class="index___td_halign_left_woynx index___td_1fTgv index__with-details_1OZkV index_tdHovered_1ZOPR index__has-background_1ZgcC"><div class="index_textBlock_20DCh">AnyText</div></td>
<td class="index___td_halign_left_woynx index___td_1fTgv index__with-details_1OZkV index_tdHovered_1ZOPR index__has-background_1ZgcC"><div class="index_textBlock_20DCh">SomeText</div></td>
<td class="index___td_halign_left_woynx index___td_1fTgv index__with-details_1OZkV index_tdHovered_1ZOPR index__has-background_1ZgcC"><div class="index_textBlock_20DCh">MoreTextHere</div></td>
</tr>
<tr class = ="index_tr_2P">
<td class="index___td_halign_left_woynx index___td_1fTgv index__with-details_1OZkV index_tdHovered_1ZOPR index__has-background_1ZgcC"><div class="index_textBlock_20DCh">AnyText2</div></td>
<td class="index___td_halign_left_woynx index___td_1fTgv index__with-details_1OZkV index_tdHovered_1ZOPR index__has-background_1ZgcC"><div class="index_textBlock_20DCh">SomeText2</div></td>
<td class="index___td_halign_left_woynx index___td_1fTgv index__with-details_1OZkV index_tdHovered_1ZOPR index__has-background_1ZgcC"><div class="index_textBlock_20DCh">MoreTextHere2</div></td>
</tr>发布于 2021-10-13 18:28:42
你可以做这样的事。
if (
Cypress.$('td div.index_textBlock_20DCh').eq(0).is(':empty') &&
Cypress.$('td div.index_textBlock_20DCh').eq(1).is(':empty') &&
Cypress.$('td div.index_textBlock_20DCh').eq(2).is(':empty')
) {
//Do Something
} else {
//Do Something
}您还可以提取文本并检查文本长度是否为0。类似于:
if (
Cypress.$('td div.index_textBlock_20DCh').eq(0).text().length == 0 &&
Cypress.$('td div.index_textBlock_20DCh').eq(1).text().length == 0 &&
Cypress.$('td div.index_textBlock_20DCh').eq(2).text().length == 0
) {
//Do Something
} else {
//Do Something
}使用each()
var countWithoutText = 0
cy.get('td div.index_textBlock_20DCh')
.each((ele, lis) => {
if (ele.text().length == 0) {
countWithoutText++
}
})
.then((lis) => {
if (lis == countWithoutText) {
//Do something all elements are empty
} else {
//Do something when all elements are not empty
}
})https://stackoverflow.com/questions/69554607
复制相似问题