如何返回 elem 的值以便我可以验证它是真的1
?
const elem = await page.$('input#my-input')
await elem.fill('1')
如何返回 elem 的值以便我可以验证它是真的1
?
const elem = await page.$('input#my-input')
await elem.fill('1')
inputValue
方法已在 Playwright v1.13.0 中添加
await page.inputValue('input#my-input');
它input.value
为选定的<input>
或<textarea>
元素返回。抛出非输入元素。阅读更多。
最简单的方法是使用$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();
})();
从 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