简体   繁体   English

如何单击一个存在两次同名的按钮?

[英]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 Cypress .get() command accepts complex selector arguments. Cypress .get()命令接受复杂的选择器参数。 So if you wanted to trigger a click on an element that has red , trash and icon classes, you could do the following:因此,如果您想触发对具有redtrashicon类的元素的点击,您可以执行以下操作:

cy.get('.red.trash.icon').click()

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.

相关问题 selenium:如果相同的按钮已存在于具有相同名称,值,id的同一页面上,如何选择单击按钮? - selenium: How to select a button to click , if the same button already exists on the same page with same name,value,id? JS切换复选框按钮一键点击两次 - JS Switch Checkbox Button clickes two times in one click 如何根据按钮的值在一个按钮中调用两个函数而不是在同一次单击中调用? - How to call two functions in one button but not in same click according to the value of the button? 如何在量角器中单击同一按钮超过50次? - How to click the same button more than 50 times in protractor? 两个jQuery函数侦听相同的按钮单击,但只有一个起作用 - two jQuery functions listening to the same button click but only one works 用于按钮btnCsv的AsyncPostBackTrigger多次调用,一次单击可多次下载同一Csv文件 - AsyncPostBackTrigger for button btnCsv calling multiple times and downloading same Csv file multiple time in one click 如何在单击按钮时将 class 添加到具有相同名称的 div - How to add class to div having same name on button click 如何使用相同的按钮使用.click两个函数? - How to have .click on two functions using the same button? 如何连接单击添加按钮创建的同一组件中的两个组件 - How to connect two components in a same component created on click of add button 如何使用 jQuery 在按钮单击时添加具有相同 ID 的两行或多行 - How to add with jQuery two or more rows with same ID on button click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM