Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value for data-id in cypress

I need to get the value for data-id but when I use the below code it does not return any.

cy.get('[data-nemo=token]')
  .invoke('attr', 'data-id').then(dataId => {
    cy.log('dataId : ', dataId);`enter code here`
  });

Thanks,

like image 810
Kavita Rajkumar Avatar asked Nov 07 '25 09:11

Kavita Rajkumar


1 Answers

As per Cypress docs, you can use cy.invoke to call a jQuery method. So, just as in jQuery, you would do

$('[data-nemo=token]').data('id');

in Cypress it's

cy.get('[data-nemo=token]').invoke('data', 'id');

If you then want to use that value, you can either use .then or create an alias with .as and reference the alias later in your tests:

cy.get('[data-nemo=token]')
  .invoke('data', 'id')
  .then(dataId => cy.log('dataId : ', dataId));

or

cy.get('[data-nemo=token]')
  .invoke('data', 'id')
  .as('dataId');

cy.get('@dataId')
  .then(dataId => cy.log('dataId : ', dataId));
like image 141
Derek Henderson Avatar answered Nov 10 '25 16:11

Derek Henderson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!