1

I ran the following code:

await page.goto("http://github.com/", { "waitUntil": "networkidle2" });
let divs = await page.evaluate(() => $("div"));

But this throws the following error:

Error: Evaluation failed: ReferenceError: $ is not defined.

But when I check, it is defined:

enter image description here

vsync
  • 118,978
  • 58
  • 307
  • 400
AskYous
  • 4,332
  • 9
  • 46
  • 82

1 Answers1

3

$() is not a standard function in JS, it's only a helper in Chrome devtools (page.evaluate is not the same environment as Chrome Devtools):

Note: This API is only available from within the console itself. You cannot access the Command Line API from scripts on the page.

You should use instead document.querySelector().

Here is working example:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('http://github.com/', { waitUntil: 'networkidle2' });

  const textOfFirstDiv = await page.evaluate(
    () => document.querySelector('div').textContent
  );
  console.log(textOfFirstDiv);

  await browser.close();
})();

Edit

You choose unfortunate example to explain your problem. If you want to extract JS value from page use it simply like this (Wikipedia has custom Geo value in window object)

await page.goto('https://en.wikipedia.org');
const geo = await page.evaluate(() => Geo);
console.log(geo);
Everettss
  • 15,475
  • 9
  • 72
  • 98
  • I need to read the value of a variable such as `$`. I'm not trying to get DOM elements. It was just an example. I do I read the value of a variable? – AskYous Mar 03 '18 at 17:51
  • @AskYous I hope now I answered a question you are looking for. – Everettss Mar 03 '18 at 18:07
  • Yeah it's better, but in my question, I tried to read the value of `$('div')` but puppeteer said `$ is not defined` while it actually was defined. So why can't I read the value of the variable `$` on github.com using puppeteer? I used this code: `await page.evaluate(() => $);`. This was my initial question. – AskYous Mar 03 '18 at 18:10
  • @AskYous GitHub doesn't have any `$` function, I told you - It's only a helper in devtools. – Everettss Mar 03 '18 at 18:13
  • sorry I'm really confused now. When I go to GitHub, I typed `$` in the console and it **is** a function: https://i.imgur.com/RoxjQJg.png. I'm missing something. – AskYous Mar 03 '18 at 18:17
  • @AskYous correct my terrible mistake in a question: it was `x()` but should be `$()` – Everettss Mar 03 '18 at 18:17
  • Ooooh... I can call **only** standard functions and variables in puppeteer.evaluate? – AskYous Mar 03 '18 at 18:18
  • @AskYous Command Line API functions belong only to DevTools (GUI in normal Chorme). This question has the same roots as yours https://stackoverflow.com/questions/48448586/how-to-use-xpath-in-chrome-headlesspuppeteer-evaluate – Everettss Mar 03 '18 at 18:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166160/discussion-between-askyous-and-everettss). – AskYous Mar 03 '18 at 18:41