9

如何返回 elem 的值以便我可以验证它是真的1

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

3 回答 3

15

inputValue方法已在 Playwright v1.13.0 中添加

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

input.value为选定的<input><textarea>元素返回。抛出非输入元素。阅读更多

于 2021-07-23T08:42:00.637 回答
8

最简单的方法是使用$eval. 在这里你可以看到一个小例子:

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();
})();
于 2020-05-25T12:29:10.717 回答
2

从 1.19 版(可能还有更低的版本)开始,不推荐使用 Element Handler。取而代之的是使用定位器。

page.locator(selector).innerText()

在你的情况下,它会是

expect(page.locator("input#my-input").innerText().includes("1")).toBeTruthy()

阅读更多: https ://playwright.dev/docs/api/class-elementhandle#element-handle-fill

于 2022-02-19T18:05:20.107 回答