正如@RosenMihaylov 所说,您可能会发现使用遵循 HTML 结构的赛普拉斯“关系”命令比使用 CSS 选择器更容易。
另外,我认为您需要点击两次——一次打开菜单,第二次调用删除操作。
第 1 步 - 看起来文字 devEnv_admin 标识了您想要的卡
cy.contains('div', 'devEnv_admin')
这会给你第 7 个 div。
第 2 步 - 您需要单击的下拉菜单是上述的第二个同级
.siblings('div.note-event__dropdown') // list of siblings matching the selector
.eq(0) // take the first (and only)
它为您提供下拉按钮的父级。
第 3 步 - 但看起来具有 button 类的子元素可能具有单击事件处理程序(您可能必须在此处进行试验,因为有时很难找到具有事件处理程序的元素)
.children('div.button') // list of children matching the selector
.eq(0) // take the first (and only)
.click();
应该会打开菜单,但可能需要几毫秒才能出现
第 4 步 - 等待包含所需文本的 div
cy.contains('span', 'Delete yield') // this form of .contains() waits for the text
.click();
总之,
cy.contains('div', 'devEnv_admin')
.siblings('div.note-event__dropdown') // list of siblings matching the selector
.eq(0) // take the first (and only)
.children('div.button') // list of children matching the selector
.eq(0) // take the first (and only)
.click();
cy.contains('span', 'Delete yield') // this form of .contains() waits for the text
.click();
您可以使用通过 DOM 元素和其他选择器的其他路径,例如 .next() 和 .parent()。
很大程度上取决于事件处理程序的附加位置,通过查看源代码最容易找到。
或者,使用within()
cy.contains('.yield-event__date-and-text--container', 'devEnv_admin') // pick the card
.within(() => { // restricts commands below to this card
cy.get('div.button.dropdown').click();
cy.contains('span', 'Delete yield').click();
});