question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug]: Using $eval or evaluate selector to get child elements results in missing HTML properties

See original GitHub issue

Bug description

Hello, when I try to use await page.$eval('.container', e => e.children) to select a nested element like the one below:

 <div class="container">
    <div class="child">A</div>
    <div class="child">B</div>
    <div class="child">C</div>
  </div>

I do not get the element’s available properties. For example , when I selected an element with children and want to get its innerHTML or innerText. All I end up with is after converting to an array from a HTMLCollection is:

[
  {  vei: { onClick: [Object] } },
  {  vei: { onClick: [Object] } },
  {  vei: { onClick: [Object] } },
  {  vei: { onClick: [Object] } },
  {  vei: { onClick: [Object] } },
  {  vei: { onClick: [Object] } },
  {  vei: { onClick: [Object] } },
  {  vei: { onClick: [Object] } },
  {},
  { vei: { onClick: [Object] } },
  { vei: { onClick: [Object] } }
]

This is a list of buttons for a paginated navigation element and I would like to get the innerText properties for each element so I can access the page numbers, but only the onClick event handler property is accessible. Selecting the elements using the selector individually I am able to access all of its properties. I’ve also tried using

await queryLoadedPage.evaluate(() => { 
      let elem = document.querySelector('.container');
      let children = elem?.children
      console.log(children)
   });

But this just returns undefined for the children. Any help would be greatly appreciated!

Puppeteer version

19.4.0

Node.js version

v18.12.1

npm version

9.1.3

What operating system are you seeing the problem on?

Linux, Windows

Configuration file

No response

Relevant log output

No response

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
OrKoNcommented, Dec 8, 2022

What I mean is

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch({
        headless: true,
    });
    const page = await browser.newPage();
    await page.setContent(`
    <div class="container">
        <div class="child">A</div>
        <div class="child">B</div>
        <div class="child">C</div>
    </div>
    `)
    let children = await page.$eval('.container', e => {
        const data = [];
        for (const child of e.children) {
            data.push({ tagName: child.tagName, innerText: child.innerText });
        }
        return data;
    });
    console.log(children); // [ { tagName: 'DIV', innerText: 'A' }, { tagName: 'DIV', innerText: 'B' }, { tagName: 'DIV', innerText: 'C' }]
    await browser.close();
})()  

otherwise you get the default representation of the DOM Element as a string.

0reactions
OrKoNcommented, Dec 8, 2022

@JohnnyRacer better ask on StackOverflow. Short answer: the same way, either using selectors or DOM APIs in the evaluated function.

Read more comments on GitHub >

github_iconTop Results From Across the Web

page.$eval() not finding selector, selector works in console
I have tried using page.$eval(), page.evaluate() and page.evaluateHandle() using document.querySelector(). const url = await page ...
Read more >
Puppeteer documentation - DevDocs
This method runs document.querySelector within the page and passes it as the first argument to pageFunction . If there's no element matching selector...
Read more >
Uncaught TypeError: Cannot read property of null - iDiallo
This will result in Uncaught TypeError: Cannot read property 'value' ... The reason will be that the element with id input does not...
Read more >
How to Read React Errors (fix 'Cannot read property of ...
The Quick Fix. This error usually means you're trying to use .map on an array, but that array isn't defined yet.
Read more >
A Complete Guide to Custom Properties | CSS-Tricks
Custom properties must be within a selector and start with two dashes ... The idea is to apply all the transforms an element...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found