Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting value of input element in Playwright

Tags:

playwright

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')
like image 743
JakeDK Avatar asked May 25 '20 12:05

JakeDK


2 Answers

inputValue method has been added in Playwright v1.13.0

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

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

like image 53
Yevhen Laichenkov Avatar answered Sep 28 '22 18:09

Yevhen Laichenkov


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();
})();
like image 45
Max Schmitt Avatar answered Sep 28 '22 18:09

Max Schmitt