Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get entire Playwright page in html and Text

I am using playwright in nodejs and I am having some problems when getting the page Text or Html. I just want to get the url as string like: <html><div class="123"><a>link</a>something</div><div>somethingelse</div></hmtl>

const browser = await playwright.chromium.launch({
    headless: true,
});

const page = await browser.newPage();
await page.goto(url);

I was trying to use const pageText = page.$('div').innerText; and also const pageText2 = await page.$$eval('div', el => el.innerText); But both do not work and just give me undefined.

like image 596
Hygison Brandao Avatar asked Jan 24 '26 00:01

Hygison Brandao


1 Answers

For the full html of the page, this is what you need: const html = await page.content()

To get the inner text of the div, this should work: const pageText = await page.innerText('div')

See:

  • https://playwright.dev/docs/api/class-page#page-content
  • https://playwright.dev/docs/api/class-page#page-inner-text
like image 68
refactoreric Avatar answered Jan 25 '26 13:01

refactoreric



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!