5

I would like to get all the elements in my DOM with a specific css path:

var elements = await chromeless.evaluate(() => document.querySelectorAll('div a'))
console.log(elements[0].innerHTML)
console.log(elements[1].innerHTML)

but this code gives me the error "Object reference chain is too long" on the first line

This code works though:

var element = await chromeless.evaluate(() => document.querySelectorAll('div a')[0].innerHTML)
console.log(element)

and I could potentially use a loop to retrieve them all but I have no idea how many elements have this css in my DOM so I don't know how many times to loop.

What's the correct syntax to get all the desired elements?

Sulli
  • 763
  • 1
  • 11
  • 33
  • Stack Snippets says `await is only valid in await function` – vrintle Sep 30 '18 at 10:06
  • @rv7 I'm running this code in an async function `async function run() { const chromeless = new Chromeless() (code)}` – Sulli Sep 30 '18 at 11:41
  • @sulli - having exactly the same issue. I can reproduce it in browser (that is, getting the same "Object reference chain ..." error. The trouble is that what I *can* make work in the browser I can't make work in Chromeless. May I ask what site you're trying to do this for? – mistertee Oct 10 '18 at 09:43
  • 1
    So after a bit of experimentation and some guesswork... my theory is that the problem lies with returning a nodelist rather than an actual array - if I try to stringify the nodelist I get the error but if I .map() over the list and return the attrs I want then I don't get the error - maybe you could try that? – mistertee Oct 10 '18 at 09:50

1 Answers1

1
const elements = await chromeless.evaluateHandle(() => {
  const allOccurances = [...document.querySelectorAll("div a")];
  const data = allOccurances.map((node) => node.innerHTML);
  return data;
});
const response = await elements.jsonValue();

console.log(response);

Instead of chromeless we can use page by creating a page as per puppeteer documentation https://pptr.dev/#?product=Puppeteer&version=v13.1.3&show=api-class-page