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

687
Vistas
Get attributes value in Puppeteer using xpath

I am using Puppeteer to automate a UI. I need to fetch an attribute value of a certain element, and I am using xpath to locate the element.

xpath:

//div[text()="5546800"]//following::div[@col-id="21"] .

I am getting an error "Failed to execute 'queryselector' on 'Document:

//div[text()="5546800"]//following::div[@col-id="21"] is not a valid selector

Below is the code I used:

const attributes=await page.$eval('//div[text()="5546800"]//following::div[@col-id="21"]',el =>el.getAttributes('type'))
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can't use XPath expressions in page.$eval, but you can create elementHandle with page.$x that can be passed to page.evaluate:

const attributes = await page.evaluate(el => el.getAttribute('type'), (await page.$x('//div[text()="5546800"]//following::div[@col-id="21"]'))[0])

FYI: I am not sure if you wanted to use getAttributes() and not getAttribute() or getAttributeNames().

Edit

Sorry, I made two typos in the previous version of this answer:

  • fixed closing single quote
  • XPath $x returns an array of element handles (just like $$ and not like $) so you need to use an index of the desired element. You also need to group the await-ed expression with parenthesis! await page.$x('//div[text()="5546800"]//following::div[@col-id="21"]') => (await page.$x('//div[text()="5546800"]//following::div[@col-id="21"]'))[0]
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