Playwright - how to print element to HTML?

Viewed 1033

I'm using Playwright with node.js. I've found an element with page.$$, how do I save its outer HTML to string?

2 Answers

If you have more element handles you got using page.$$() and you want to get an outer html of each, this would be a way:

const outerHtmls = await page.$$eval('.myElement', el => el.outerHtml);

More information about page.$$eval() could be found here.

Playwright doesn't have an outerHTML() function. It sounds like you already have an ElementHandle. If so, try this!

Javascript

async function outerHtml(elem) {
    if(!elem) {
        return undefined;
    }

    return await elem.evaluate(elem => elem.outerHTML);
}

Typescript

async outerHtml(elem: ElementHandle) {
    if(!elem) {
        return undefined;
    }

    //@ts-ignore
    return await elem.evaluate(elem => elem.outerHTML);
}
Related