Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cy.click() can only be called on a single element

I am trying to select two buttons one after one , a Delete button and after that the Yes popup confirmation ,by this:

cy.get('.btn-danger').last().click();
cy.get('.btn-primary').click();

But i got this error:

CypressError: cy.click() can only be called on a single element.
Your subject contained 2 elements.
Pass { multiple: true } if you want to serially click each element.

like image 554
Yielda Toro Avatar asked Sep 05 '19 11:09

Yielda Toro


People also ask

How do you click twice in Cypress?

Double click all buttons found on the page By default, Cypress will iteratively apply the double click to each element and will also log to the Command Log multiple times. You can turn this off by passing multiple: false to . dblclick() .

How do you use multiple true Cypress?

Click all elements with id starting with 'btn' By default, Cypress will error if you're trying to click multiple elements. By passing { multiple: true } Cypress will iteratively apply the click to each element and will also log to the Command Log multiple times.


2 Answers

I think there are two buttons with btn-primary class in your popup DOM (yes and cancel?). Try to access the yes confirmation button by its id or something. Or if you're sure about the order, then use .first() or .last() like you used for clicking the delete button.

like image 116
DurkoMatko Avatar answered Oct 24 '22 01:10

DurkoMatko


Another method is with .eq(index) assuming you have array of two buttons [0, 1] and last being 1

cy.get('.btn-danger').eq(0).click();
cy.get('.btn-primary').eq(1).click();

Check for documentation Eq function.

like image 2
Murshid Hassen Avatar answered Oct 24 '22 02:10

Murshid Hassen