Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

693
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda