Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

742
Vistas
How to select random option from dropdown in cypress

I am trying to choose a random option from a dropdown list. No answers i found so far helped. I have no issues selecting a specific option, since all values have IDs set.

This is simple/straightforward and its working:

const log = new loginPage()
            log.visit()
            log.loginEmail('test')
            log.loginPassword('test')
            log.submit()
            cy.get('#link-new-application').click()
            cy.get('#control-nationality').click()
            cy.get('#option-nationality-DZA').click()

Any advice for random selection? I am a beginner so any help would be much appreciated.

This is HTML: enter image description here

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Cypress comes with Lodash baked in.

Lodesh brings a lot handy utility methods, with the sample method you can get a random item from a collection.

https://docs.cypress.io/api/utilities/_#Syntax

https://lodash.com/docs#sample

cy.get('id^="option-nationality-"]')
  .then(options => {
    cy.get(Cypress._.sample(options)).click()
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

One approach is to get all your options then click one randomly

cy.get('#control-nationality').click()  // open the dropdown

cy.get('id^="option-nationality-"]')    // all elements with id starting "option-nationality"
  .then($options => {
    const count = $options.length
    const randomIndex = Math.floor(Math.random() * count)
    $options.eq(randomIndex).click()
  })
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda