Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

685
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!