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

492
Views
puppeteer - Error: Evaluation failed: DOMException: Failed to execute 'querySelector'

I'm trying to select some nested divs using puppeteer $$eval. I've copied the XPath of the DOm elements I need to select but seems not working.

    //XPath //*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a/span
    await page.waitForSelector('//*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a/span')
    const names = await page.$$eval('//*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a/span', (el) => {
        return el.map( n => n.innerHTML )
    })
    citiesList.push(names)

    //XPath //*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a
    await page.waitForSelector('//*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a')
    const href = await page.$$eval('//*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a', (el) => {
        return el.map( a => a.getAttribute('href') )
    })
    citiesLinks.push(href)

With this code I will get always this error

Error: Evaluation failed: DOMException: Failed to execute 'querySelector' on 'Document': '//*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a/span' is not a valid selector

How I can use xpath with $$eval correctly?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I've solved by targeting directly the DOM elements I need. In my case I have an li tag with .list-item bootstrap class.

 await page.waitForSelector('li.list-item > a', (el) => {
   return el.map( a => a.getAttribute('href') )
 })
 citiesLinks.push(href)
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!