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

332
Views
How can I use jQuery with Puppeteer?

So, I am using Puppeteer (a headless browser) to scrape through a website, and when I access that url, how can I load jQuery to use it inside my page.evaluate() function.

All I have now is a .js file and I'm running the code below. It goes to my URL as intended until I get an error on page.evaluate() since it seems like it's not loading the jQuery as I thought it would from the code on line 7: await page.addScriptTag({url: 'https://code.jquery.com/jquery-3.2.1.min.js'})

Any ideas how I can load jQuery correctly here, so that I can use jQuery inside my page.evaluate() function?

(async() => {
  let url = "[website url I'm scraping]"
  let browser = await puppeteer.launch({headless:false});
  let page = await browser.newPage();
  await page.goto(url, {waitUntil: 'networkidle2'});
  // code below doesn't seem to load jQuery, since I get an error in page.evaluate()
  await page.addScriptTag({url: 'https://code.jquery.com/jquery-3.2.1.min.js'})
  await page.evaluate( () => {
      // want to use jQuery here to do access DOM
      var classes = $( "td:contains('Lec')")
      classes = classes.not('.Comments')
      classes = classes.not('.Pct100')
      classes = Array.from(classes)
  });
})();
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You are on the right path.

Also I don't see any jQuery code being used in your evaluate function. There is no document.getElement function.

The best way would to be to add a local copy of jQuery to avoid any cross origin errors.

More details can be found in the already answered question here.

UPDATE: I tried a small snippet to test jquery. The puppeteer version is 10.4.0.

(async () => {
    const browser = await puppeteer.launch({headless:false});
    const page = await browser.newPage();
    await page.goto('https://google.com',{waitUntil: 'networkidle2'});
    await page.addScriptTag({path: "jquery.js"})
    await page.evaluate( () => {
        let wrapper = $(".L3eUgb");
        wrapper.css("background-color","red");
    }) 
    await page.screenshot({path:"hello.png"});
    await browser.close();
})();

The screenshot is

puppeteer image

So the jquery code is definitely working.

Also check if the host website doesn't have a jQuery instance already. In that case you would need to use jquery noConflict

$.noConflict();
about 4 years ago · Juan Pablo Isaza Report

0

Fixed it!

I realized I forgot to include the code where I did some extra navigation clicks after going to my initial URL, so the problem was from adding the script tag to my initial URL instead of after navigating to my final destination URL.

I also needed to use

await page.waitForNavigation({waitUntil: 'networkidle2'})

before adding the script tag so that the page was fully loaded before adding the script.

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!