user
user

Reputation: 1759

How to get the value of an attribute in cypress

I have this HTML element, and I am trying to get the value of for attribute

<div class="test">
  <label for="aboqo_46" data-test = "user-test" >

I want to get retrieve the value in for which is aboqo_46 in the above code. How can this be achieved?

I have tried the following but could not get the values.

const result = cy
                 .get('[data-test="user-test"]')
                 .invoke('attr','for')
cy.log(result)

The above code logs the result value as Object{5}

and

cy
 .get('[data-test="user-test"]')
 .its('for')

Upvotes: 1

Views: 355

Answers (1)

Fody
Fody

Reputation: 31954

Your code is a little off, it's not returning the attribute value it's returning a Chainer object so that Cypress can chain commands.

This will work:

cy.get('[data-test="user-test"]')
  .invoke('attr','for')
  .then(value => cy.log(value))

Upvotes: 1

Related Questions