[英]How to click one button that exists two times with same name?
I try to click a button named .red and the problem is that there are two buttons with same name so Cypress does not know which of them to click with command cy.get('.red').click()我尝试单击一个名为 .red 的按钮,但问题是有两个按钮具有相同的名称,因此赛普拉斯不知道使用命令 cy.get('.red').click() 来单击其中的哪个按钮
Originally I thought I have to access my class before trying to click the button.最初我认为我必须在尝试单击按钮之前访问我的课程。
How can I use below code to click "red trash icon"?如何使用以下代码单击“红色垃圾桶图标”?
<div class="one wide column">
<div class="ui vertical right floated buttons">
<a class="ui basic button" role="button" href="/admin/assignments/edit/37">
<i aria-hidden="true" class="cog icon"></i>
</a>
<button class="ui basic button">
<i aria-hidden="true" class="red trash icon"></i>
</button>
</div>
</div>
The red trash icon
is the <i>
element of inside the button, not the button itself. red trash icon
是按钮内部的<i>
元素,而不是按钮本身。
You should perform click on the button itself:您应该单击按钮本身:
cy.get('.button').click()
And if there is more than one button with the selector you have set you can give it a specific class or a specific id:如果你设置的选择器有多个按钮,你可以给它一个特定的类或一个特定的 id:
<button id="whatever">...
cy.get('#whatever').click()
Notice the use of #
for ids instead of the dot .
请注意
#
用于 ids 而不是点.
as a class selector.作为类选择器。
The problem is you're using a too broad CSS selector.问题是您使用的 CSS 选择器过于宽泛。
You could either set a unique ID html attribute to your button and find it with cy.get('#your-id')
or you could make a more specific CSS selector.您可以为您的按钮设置一个唯一的 ID html 属性并使用
cy.get('#your-id')
找到它,或者您可以制作一个更具体的 CSS 选择器。
For example you could select the last button with .red class like this例如,您可以像这样选择带有 .red 类的最后一个按钮
cy.get('button.red:last-of-type')
or the first one line this或者第一行这个
cy.get('button.red:first-of-type')
or the X element using something like button.red:nth-of-type() { ... }或 X 元素使用类似 button.red:nth-of-type() { ... }
按照 Cypress 文档中的建议,您应该添加自定义data-cy
属性而不是使用通用选择器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.