Error trying to get attribute from element in Cypress

Now there is a plugin for your need.

https://github.com/Lakitna/cypress-commands/blob/develop/docs/attribute.md

With this, you'll be able to do :

cy.get('input').attribute('placeholder').should('contain', 'username');

If above answers doesn't work try this,

cy.get('input[placeholder*="Name"]')

Find the input with a placeholder attribute containing the word "Name".


invoke() calls a jquery function on the element. To get the value of an input, use the function val():

cy.get('input').invoke('val').should('contain', 'mytext')

This is not the same as getting the value attribute which will not update with user input, it only presets the value when the element renders. To get an attribute, you can use the jquery function attr():

cy.get('input').invoke('attr', 'placeholder').should('contain', 'username')

you can use this

cy.get('a') // yields the element
  .should('have.attr', 'href') // yields the "href" attribute
  .and('equal', '/home') // checks the "href" value

or

cy.get('a').should('have.attr', 'href', '/home')

for more details check this: https://docs.cypress.io/api/commands/should#Method-and-Value

Tags:

Cypress