Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playwright - how to print element to HTML (.outerHTML)?

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

like image 721
eeeeeeeeeeeeeeeeeeeeeeeeeeeeee Avatar asked Oct 25 '25 18:10

eeeeeeeeeeeeeeeeeeeeeeeeeeeeee


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.

like image 110
pavelsaman Avatar answered Oct 28 '25 09:10

pavelsaman


Use locator.

Use of $$ is discouraged per latest playwright docs.

const tweets = page.locator('.tweet .retweets');
expect(await tweets.evaluate(node => node.outerHTML)).toBe('10 retweets');
like image 27
Vishal Aggarwal Avatar answered Oct 28 '25 10:10

Vishal Aggarwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!