Skip to content

document.getElementsByTagName returns object tree with empty values? #1263

Closed
@webchaz

Description

@webchaz

I have the following code:

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://www.amazon.com');

  const a = await page.evaluate(() => {
    return document.getElementsByTagName('a')
  });
  console.log(a)
})();

This results in a long list of:

{
  '0': {},
  '1': {},
  '2': {},
  '3': { jQuery16409950154717072515: 199 },
  '4': { jQuery16409950154717072515: 191 },
  '5': {}
}

How do I get the actual DOM from this?

Activity

ebidel

ebidel commented on Nov 2, 2017

@ebidel
Contributor

You can't return the actual page DOM back into a puppeteer script. Those objects live in separate worlds. What you get from page.evaluate() is a JSONHandle that you can actual on. By default, this gets serialized (if it can be). So that's what you're seeing.

jsonValue() and getProperties() are useful for acting on the return object. For example, if you wanted to get all of the hrefs:

const list = await page.evaluateHandle(() => {
  return Array.from(document.getElementsByTagName('a')).map(a => a.href);
});
console.log(await list.jsonValue());

What are you trying to do?

ernstbolt

ernstbolt commented on Dec 15, 2017

@ernstbolt

Thank you! Just what I was looking for!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ebidel@aslushnikov@webchaz@ernstbolt

        Issue actions

          document.getElementsByTagName returns object tree with empty values? · Issue #1263 · puppeteer/puppeteer