Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select nth item inside select element in cypress

Tags:

cypress

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?

like image 350
JD. Avatar asked Jun 07 '18 22:06

JD.


People also ask

How do you get the next element in Cypress?

next() To get the next sibling DOM element within elements, use the . next() command.

How do you get a dropdown value in Cypress?

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.


1 Answers

Update

As pointed out by @dpstree in the comments, this doesn't answer the original question. Please see more recent answers for a complete solution.

Original

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' 
like image 94
Arnaud P Avatar answered Sep 22 '22 02:09

Arnaud P