Converting an ElementHandle to a DOM element using puppeteer?
Use ElementHandle.evaluate()
:
const elementHandle = await page.waitForSelector('.selector-list li')
elementHandle.evaluate((domElement) => {
domElement.tagName
// etc ...
})
Typescript:
const elementHandle: ElementHandle = await page.waitForSelector('.selector-list li')
elementHandle.evaluate((domElement) => {
domElement.tagName
// etc ...
})
The better way would be to execute the code on the page via page.evaluate and return the results. That way you can return an array with values:
const result = await page.evaluate(() => {
const elements = document.querySelectorAll(".selector-list li");
// do something with elements, like mapping elements to an attribute:
return Array.from(elements).map(element => element.tagName);
});
result
will then be an array with the attribute values of tagName
of each element.