'how to find href attribute to an element using cypress testing library?

using findByRole I get this error because of multiple matches on search my parameter.

Timed out retrying after 5000ms: Found multiple elements with the role "link" and name /eligibility/i

Here are the matching elements:

<a
  href="/HHIN/PBEligibility/EligibilitySearch.aspx"
>
  Eligibility
</a>

<a
  href="/HHIN/RiderEligibility/EligibilitySearch.aspx"
>
  Rider Eligibility
</a>

<a
  href="/HHIN/QUESTEligibility/EligibilitySearch.aspx"
>
  Eligibility
</a>

<a
  href="/HHIN/BXEligibility/EligibilitySearch.aspx"
>
  Eligibility
</a>

(If this is intentional, then use the AllBy variant of the query (like queryAllByText, getAllByText, or findAllByText)).

using findAllByRole then within method returns all 5 elements, but then using the findByText method doesn't single out the element i need(pbeligibility). How can I query by href attribute to find that unique value string 'pbeligibility'? or any other way to click on that first eligibility index? I know how to do it using native cypress methods but I want to do it using the testing library methods.

  it('this will login to HHIN', () => {
    cy.log('Opening HHIN Login Page');
    cy.visit("URL");

    cy.log('Entering in Username/Password');
    cy.findByRole('row', { name: /User/i }).should('exist');
    cy.findByRole('textbox', { input: /UserId/i }).type('username');
    cy.findAllByRole('row', { name: /password/i }).type('password');
    cy.findByRole('button', { name: /login/i }).click();
    cy.get('body').then((body) => {
      if (body.find('input#ctl00_cphContent_btContinue').length > 0) {
        cy.get('input#ctl00_cphContent_btContinue').click();
      } else {
        cy.title().should('eql', 'HHIN');
      }
      cy.findAllByRole('link', { name: /eligibility/i }).within(() => {
        cy.findByText(/PBelegibility/i).click();
      });
    });

    // const pbEligObj = cy.findAllByRole('link', { name: /eligibility/i });
    // cy.log(pbEligObj);

    // for (let key in pbEligObj) {
    //   if (pbEligObj.hasOwnProperty(key)) {
    //     let value = pbEligObj[key];
    //     cy.log(key, value);
    //   }
    // }
  });
});    


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source