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?
By using eq
I had the same problem and solved it by creating a custom cypress command. No as pretty as I would like, but it did the job.
then I used in the test as such
The
option +option
part was the only way I could find to get the full list of options inside a select and it's currently the bit of code i'm trying to work arround although it works fine.Based on solution from Miguel Rueda, but using
prevSubject
option:Usage:
Explanation:
children('option')
and.eq(pos)
traverse children of select to specific element.select
method with value of selected element.In the particular context of selecting the nth
option
, this may be appropriate: