Playwright: [Question] - page.waitForFunction

Created on 19 Apr 2020  路  2Comments  路  Source: microsoft/playwright

Is there a way for page.waitForFunction() to return a value from evaluated script?
OR there is another way to use that functionality on another function?

I want to take a screenshot on a lazyloaded site and I need some info sent from parsed script on page. THANK YOU!

Most helpful comment

Yep, there is a way via the JSHandle.

const playwright = require("playwright");

(async () => {
  const browser = await playwright.webkit.launch();
  const page = await browser.newPage();
  await page.evaluate(() => document.write(`
    <div id="foo">bar</div>
  `))
  const handle = await page.waitForFunction(() => {
    return document.getElementById("foo").textContent
  })
  const value = await handle.jsonValue()
  console.log(value)
})();

Or interactive: https://try.playwright.tech/?s=oyh6u

All 2 comments

Yep, there is a way via the JSHandle.

const playwright = require("playwright");

(async () => {
  const browser = await playwright.webkit.launch();
  const page = await browser.newPage();
  await page.evaluate(() => document.write(`
    <div id="foo">bar</div>
  `))
  const handle = await page.waitForFunction(() => {
    return document.getElementById("foo").textContent
  })
  const value = await handle.jsonValue()
  console.log(value)
})();

Or interactive: https://try.playwright.tech/?s=oyh6u

@mxschmitt thank you! it works

Was this page helpful?
0 / 5 - 0 ratings