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

249
Vistas
how to return a couple of value from a querySelector?

I'm trying to get an object with the value and the inner text from a dropDown menu using a query selector:

<label>
  <span>Project :</span>
</label>
<select name="projectId">
  <option value="1">foo</option>
  <option value="2">bar</option>
</select>

I want an object that look like this:

{
    "1":"foo",
    "2":"bar"
}

here's where I come from

let options = await page.evaluate(() => {
                return document.querySelector('select[name=projectId]').innerText;
            });

but I only get a string of the inner text I can't figure out how to achieve my goal, can anyone help ?

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

0

You can use document.querySelectorAll to get all the option elements, spread syntax to convert it to an array, Array#map to extract the value and text of each option, and Object.fromEntries to convert it to a single object.

const res = Object.fromEntries(
  [...document.querySelectorAll("select[name=projectId] > option")]
    .map(o => [o.value, o.text]));
console.log(res);
<label>
  <span>Project :</span>
</label>
<select name="projectId">
  <option value="1">foo</option>
  <option value="2">bar</option>
</select>

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to get multiple elements via a query selector, you can use querySelectorAll. It looks like you want to do something along these lines:

const options = document.querySelectorAll('[name="projectId"] option');

const optionData = {};
for (let option of options) {
  optionData[option.value] = option.innerText;
}

console.log(optionData);
<label>
  <span>Project :</span>
</label>
<select name="projectId">
  <option value="1">foo</option>
  <option value="2">bar</option>
</select>

about 4 years ago · Juan Pablo Isaza Denunciar

0

let options = await page.evaluate(() => {

  // Get all option items
  const options =  Array.from(document.querySelectorAll('select[name=projectId] option'));
  // Iterate through all option items and set corresponding object properties
  return options.reduce((acc, item) => {
    return {...acc, ...{[item.value]: item.innerText}}
  }, {})

});
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