inputValue(selector[, options])
警告
请使用基于 locator 的
locator.inputValue([options])
代替。
返回选定的 input
、textarea
或 select
元素的 input.value
值。
参数 | 类型 | 默认值 | 描述 |
---|---|---|---|
selector | string | '' | 用于搜索元素的 selector。如果 selector 匹配多个元素,则使用第一个匹配项。 |
options | object | null | |
options.strict | boolean | false | 当 true 时,要求 selector 只匹配一个元素。如果给定的 selector 匹配多个元素,则调用会抛出异常。 |
options.timeout | number | 30000 | 最大超时时间(毫秒)。传递 0 可禁用超时。默认值会被 BrowserContext 或 Page 上的 setDefaultTimeout 选项覆盖。 |
返回值
类型 | 描述 |
---|---|
Promise<string> | 一个 Promise,当元素输入值获取成功时,它将兑现该值。 |
示例
import { browser } from 'k6/browser';
export const options = {
scenarios: {
browser: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const page = await browser.newPage();
await page.goto('https://test.k6.io/browser.php');
await page.fill('#text1', 'Hello world!');
const inputValue = await page.inputValue('#text1');
console.log(inputValue);
}