Getting value of input element in Playwright

Viewed 17735

How do I return the value of elem so that I can verify that it is in fact 1?

const elem = await page.$('input#my-input')
await elem.fill('1')
3 Answers

inputValue method has been added in Playwright v1.13.0

await page.inputValue('input#my-input');

Locator:

await page.locator('input#my-input').inputValue();

It returns input.value for the selected <input> or <textarea> element. Throws for non-input elements. Read more.

The easiest way is to use $eval. Here you see a small example:

const playwright = require("playwright");

(async () => {
  const browser = await playwright.chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();
  await page.setContent(`<input id="foo"/>`);
  await page.type("#foo", "New value")
  console.log(await page.$eval("#foo", el => el.value))
  await page.screenshot({ path: `example.png` });
  await browser.close();
})();
Related