Is there a way in Cypress.io to select a specific child of a element as opposed to using the containing text or value? In my case, the data changes and thus the test will break if used with a different set of data that is not hard-coded in.
Asked
Active
Viewed 3,100 times
2
-
1Do you want to select the nth child of an element ? If so, see [here](https://stackoverflow.com/q/50750956/777285). Otherwise please edit your question with more details on what you want to do, and what you have tried. – Arnaud P Jun 08 '18 at 14:28
1 Answers
2
Here's how you'd use select()
based on index. First you get the 4th
value of the select
, then use a .then()
that yields it's value
:
cy.get('select.myselect option').eq(4).invoke('val').then((val)=>{
cy.get('select.myselect').select(val)
})
// .eq(n) yields the nth element

kuceb
- 16,573
- 7
- 42
- 56
-
2I got this error with this approach: `Error: CypressError: Timed out retrying: cy.its() errored because the property: 'value' does not exist on your subject.` Instead, I had to replace `its('value')` with `invoke('val')` and then it works. (Cypress 3.1 docs specify using `its()` to get the value of non-function properties, and using `invoke()` to invoke an element's (jQuery) function properties. https://docs.cypress.io/api/commands/invoke.html – Mason Oct 12 '18 at 07:37