How to transform an ElementHandle into a DOM object in Puppeteer?

I am currently retrieving properties of a DOM element using an ElementHandle, as shown below:

let container = await page.waitForSelector('.selector-list li');
let elementName = await container.$eval('a', el => el.tagName);

Right now, I can access the tagName, but I want to explore additional properties without needing to call $eval for each one.

Question:

What is the method for converting an ElementHandle into a DOM element so that I can access all available properties? I specifically want the <A> element as a DOM object.

To convert an ElementHandle into a regular DOM element in Puppeteer, you can use the method elementHandle.evaluate() to pass the handle back to the context of the page. This lets you interact with the element as a standard DOM object and access multiple properties at once.

Here's how you can do it:

let container = await page.waitForSelector('.selector-list li');
let anchorElement = await container.$('a');
let properties = await page.evaluate(anchor => {
  return {
    tagName: anchor.tagName,
    href: anchor.href,
    textContent: anchor.textContent,
    // Add any other properties you need here
  };
}, anchorElement);

console.log(properties);

This approach uses page.evaluate() to pass the ElementHandle and extract all desired properties within the page context, making your script more efficient.

By doing this, you streamline your code and easily access various DOM properties without multiple $eval calls, saving time and effort.

Another effective technique to transform an ElementHandle into a DOM element involves using Puppeteer's serialization capabilities. This can provide a more direct interaction with the DOM without invoking multiple evaluations. Here's an alternative approach:

const container = await page.waitForSelector('.selector-list li');
const anchorElement = await container.$('a');

// Get properties using a single page.evaluate call
const elementDetails = await page.evaluate(anchor => {
  // Clone element to safely access properties
  const clone = {...anchor};
  return JSON.parse(JSON.stringify(clone));
}, anchorElement);

console.log(elementDetails);

In this example, we utilize the page.evaluate() function and leverage JavaScript's serialization methods to clone and access the element's properties. This allows you to interact efficiently with the DOM object, retaining all the properties you may need without repeated $eval calls.

It's essential to maintain awareness of the security implications when working with serialization in this manner; ensure the data you're manipulating complies with the necessary security protocols.

By using this strategy, you'll achieve cleaner code with fewer calls to the page context, enhancing both performance and maintainability.

Use page.evaluate() to convert an ElementHandle into a DOM element. This allows you to access all properties of the element in a single call:

let container = await page.waitForSelector('.selector-list li');
let anchorElement = await container.$('a');

let properties = await page.evaluate(anchor => ({
  tagName: anchor.tagName,
  href: anchor.href,
  textContent: anchor.textContent
}), anchorElement);

console.log(properties);

This retrieves properties without multiple calls, making it efficient.