Cannot inject jquery


#1

I am trying a puppeteer crawler in my actor and I would like to inject jquery:

await puppeteer.injectJQuery(page);
const $ = window.$;
var headLine = $("h1[itemprop='headline']").text().trim();

But I get this error:

Cannot read property ‘$’ of undefined

So it seems there is no window object?


#2

Hi,

this way works for me:

handlePageFunction: async ({ request, page }) => {
  console.log(`Processing ${request.url}...`);

  await Apify.utils.puppeteer.injectJQuery(page);

  const test = await page.evaluate(() => {
    return $('title').text().trim()
  })

  await Apify.pushData({ test });
},

#3

Seems that I have to use evaluate function when working with puppeteer crawler…