say I have the HTML:
<select name="subject" data-testid="contact-us-subject-field"> <option value="What is this regarding?">What is this regarding?</option> <option value="Partnerships">Partnerships</option> <option value="Careers">Careers</option> <option value="Press">Press</option> <option value="Other">Other</option> </select>
Selecting an option with a known value, such as 'Careers' is as easy as:
cy.get('[data-testid="contact-us-subject-field"]').select('Careers');
How do I select the nth option in this field, regardless of its value?
next() To get the next sibling DOM element within elements, use the . next() command.
Options for dropdown in Cypresslog – Default value – true− This is used to turn on/off the console log. timeout – Default value – defaultCommandTimeout(4000)− This is used to provide the maximum wait time for the selection prior to throwing an error. force – Default value – false− This is used to enforce an action.
As pointed out by @dpstree in the comments, this doesn't answer the original question. Please see more recent answers for a complete solution.
By using eq
cy.get('tbody>tr').eq(0) // Yield first 'tr' in 'tbody' cy.get('ul>li').eq(4) // Yield fifth 'li' in 'ul'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With