6

Ex:

<div data-component="tab" data-value="first_tab">
</div>

How can I get data-component="tab" and data-value="first_tab" together in cypress command? Like I want to access select box inside first_tab and I want to be more context specific and write a more readable code: What I have tried is below (which is syntactically wrong)

cy.get('[data-component="tab" data-value="first_tab"] [data-component="select_box"]').click()

2 Answers 2

13

Cypress follows the jQuery convention so this should work:

cy.get('[data-component="tab"][data-value="first_tab"]').click()

Reference: https://api.jquery.com/multiple-attribute-selector/

2
cy.get('[data-component="tab"][data-value="first_tab"]').within(() =>
    cy.get('[data-component="select_box"]').click()
    })

Cypress Docs: https://docs.cypress.io/api/commands/get.html#Get-in-within

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.