如何在cypress中获取html元素属性的值?

如何在cypress中获取html元素属性的值?,html,angular,attributes,angular8,cypress,Html,Angular,Attributes,Angular8,Cypress,我想获取cypress中元素的href属性的值,以便在测试中访问此URL 这会通过测试,但不会返回值: ShareCertificate.getSharableLink().should('have.attr', 'atn-href'); 您可以使用invoke获取属性值: ShareCertificate.getSharableLink().should('have.attr', 'atn-href').invoke('attr', 'atn-href').as('atn-href');

我想获取cypress中
元素的href属性的值,以便在测试中访问此URL

这会通过测试,但不会返回值:

  ShareCertificate.getSharableLink().should('have.attr', 'atn-href');

您可以使用invoke获取属性值:

ShareCertificate.getSharableLink().should('have.attr', 'atn-href').invoke('attr', 'atn-href').as('atn-href');
然后在需要时调用它:

cy.get('@atn-href').then(value => {
  cy.visit(value);
});

我使用jQuery解决了这个问题:

 ShareCertificate.getSharableLink().then(link => {
    const publicLink = Cypress.$(link).attr('atn-href');
    cy.visit(publicLink);
  });

谢谢,但是调用在这里不起作用。我通过使用jQuery解决了这个问题,尽管我不喜欢代码的样式:`ShareCertificate.getSharableLink().then(link=>{const publicLink=Cypress.$(link).attr('atn-href');cy.visit(publicLink);})`您是否也尝试过:
调用('prop','atn href')